当前位置:首页 > 百科中心 > IT技术 > 客户端技术

numberOfSectionsInTableView:,tableView:numberOfRow

tableview协议,如果指明遵循这个协议,一下三个函数是一定要在viewcontroller里实现的 
(1)numberOfSectionsInTableView: 
   告知视图,有多少个section需要加载到table里 
(2)tableView:numberOfRowsInSecion: 
   告知controller每个section需要加载多少个单元或多少行 
(3)tableView:cellForRowAtIndexPath: 

   返回UITableViewCell的实例,用于构成table view.这个函数是一定要实现的。 

实现的例子 

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

    

    return 1;

}

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

{

    return [self.dataSource count];

}

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

{

    static NSString *cellID = @"QuestionTypeInformationTableViewCell";

    QuestionTypeInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    

    if (cell == nil) {

        

        cell = [[QuestionTypeInformationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

                                          reuseIdentifier:cellID];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;//选中不变色

        cell.backgroundColor = [[PublicData getPulicData] getBackgroundColor];

        cell.textLabel.textColor = [[PublicData getPulicData] getTextColor];

    }

    ExamPublicData *data =  [self.dataSource objectAtIndex:indexPath.row];//section

    cell.questiontypenamelabel.text = data.data1;

    cell.questiontypeinformationlabel.text = data.data2;

    cell.delegate = self;

   

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"tableview:indexPath:row:%ld indexPath:section:%ld",(long)indexPath.row,(long)indexPath.section);

  

}

data source与delegate的区别 
data source用于提供数据给table view. 当事件发生时,table view向delegate请求,或者当table view需要更多的信息时。 
例如在以下情况下将调用delegate函数 
(1)在一个单元被选中或不再选中 
(2)当视图需要获得每个单元的高度 
(3)当视图需要构建每个section的头部及尾部 


【云广告】
本文章由网友发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

评论