uitableview有哪些必须要实现的数据源方法

2025-05-06 16:38:31
推荐回答(1个)
回答1:

  uitableview实现的数据源方法有:
  表视图继承自UIScrollView,这样的继承关系使得表视图可以实现上、下滚动。
UITableViewDatasource:实例化表视图时,必须采用该方法来实现数据源的配置
UITableViewDelegate:表视图的委托方法,一般用于处理表视图的基本样式以及捕捉选中单元格选中事件
  表视图的结构:
  表视图由头部、尾部视图,中间有一连串的单元格视图
  表视图的头部由tableHeaderView属性设置,尾部视图通过tableFooterView属性设置
  分组表格由一系列的section视图组成,每一个section又包含一个连续的单元格
  每个section视图也由头部视图和尾部视图,通过委托方法代理
  cell的使用:
  首先定义一个标示符
  其次,检查表视图中是否存在闲置的单元格,如果有取出来,没有则重新创建
  第一、UITableViewDatasource 相关的方法
  添加数据源的方法

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 获得section包含的cell个数
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
  - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different

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

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrayList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"the section is %d",indexPath.section);

static NSString *idendifier=@"cellIdentify";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:idendifier];
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idendifier];
}
cell.textLabel.text=[self.arrayList objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"andy";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"richard";
}

  表视图的编辑、移动、删除等:

  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
  - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
  - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1))
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

  - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
  第二、UITableViewDelegate相关的方法

  // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
  - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0);
  // Called after the user changes the selection.
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  sample:

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailVC=[[[DetailViewController alloc] init] autorelease];
[self.navigationController pushViewController:detailVC animated:YES];
NSLog(@"clicked");
}

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
  sampleCode:

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==1)
{
return 100;
}else
{
return 50;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 60;
}

  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
  - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
  sample code:

  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)] autorelease];
headerView.backgroundColor=[UIColor yellowColor];

UILabel *label=[[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 30)] autorelease];
label.text=@"header";
[headerView addSubview:label];
return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *headerView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)] autorelease];
headerView.backgroundColor=[UIColor redColor];

UILabel *label=[[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 30)] autorelease];
label.text=@"foot";
[headerView addSubview:label];
return headerView;