UITableView 委托方法总结

时间:2023-03-09 06:48:48
UITableView 委托方法总结
总结: UITableViewDelegate
  row:     heightForRow
  header:    heightForHeader
         viewForHeader
         titleForHeader (viewForHeader 相冲突)
  
  title:     heightForTitle
         viewForTitle
         titleForTitle (viewForTitle 相冲突)
 总结: UITableViewDatasource
  numberOfRowsInSection      (required)
      cellForRow          (required)

UITableView应用(一)UITableViewDelegate方法总结

1.定义每个UITableView中的cell的行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

// indexPath.section,根据分组进行更精确的配置

return 90.0;

}

2.设置UITableView每个分组的Header的Title

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return [_arrayType objectAtIndex:section];

}

3.设置UITableView分组Header的高

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 30.0;

}

4.设置UITableView自定义的Header

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

// 自定义的Header

ViewForCellHeader *headerView=[[[ViewForCellHeader alloc] init] autorelease];

headerView.strSectionName=[_arrayType objectAtIndex:section];

return [headerView view];

}

注意:2与4是互斥的。

同理原理,我们分别也对Footer进行设置。
5.设置UITableView每个分组的Footer的Title

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

return [_arrayType objectAtIndex:section];

}

6.设置UITableView分组Footer的高

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

// indexPath.section,根据分组进行更精确的配置

return 30.0;

}

7.设置UITableView自定义的Footer

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

// 自定义的Footer

ViewForCellHeader *headerView=[[[ViewForCellHeader alloc] init] autorelease];

headerView.strSectionName=[_arrayType objectAtIndex:section];

return [headerView view];

}

8.设置UITableView每个分组的Footer的Title

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return [_arrayType objectAtIndex:section];

}

UITableView应用(二)UITableViewDataSource 方法总结

NSMutableArray *_arrayType;// 分组

1.返回分组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [_arrayType count];

}

2.根据分组,返回每个分组的行数

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{

NSString *curType=[_arrayType objectAtIndex:section];

return [[_dictData objectForKey:curType] count];

}

3.根据分组,返回每个cell

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath
*)indexPath{

NSUInteger section = [indexPath section];

NSUInteger row = [indexPath row];

NSString *key=[_arrayType objectAtIndex:section];

NSMutableArray *arrValues=[_dictData objectForKey:key];

static NSString *CellIdentifier = @"WTVChannelCell";

// 自定义cell

WTVChannelCell *cell = (WTVChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){

cell = [[[WTVChannelCell alloc] initWithStyle:UITableViewCellSelectionStyleGray
reuseIdentifier:CellIdentifier]
autorelease];

NSArray *array =
[[NSBundle mainBundle] loadNibNamed:@"WTVChannelCell" owner:nil
options:nil];

cell =
[array objectAtIndex:0];

}

cell.dictChannelData=[arrValues objectAtIndex:row];

[cell refreshCellData];

return cell;

}