numberOfSectionsInTableView:,tableView:numberOfRowsInSecion:,tableView:cellForRowAtIndexPath:

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


实现的例子


点击(此处)折叠或打开

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  2. {
  3. NSInteger result = 0;
  4. if ([tableView isEqual:self.myTableView]){
  5. result = 3;
  6. }
  7. return result;
  8. }




点击(此处)折叠或打开

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  2. {
  3.     NSInteger result = 0;
  4.     if ([tableView isEqual:self.myTableView]){
  5.         switch (section){ 
  6. case 0:{
  7.             result = 3;
  8.             break; 
  9. }
  10.             case 1:{ 
  11. result = 5; break;
  12. }
  13.         case 2:{
  14.             result = 8;
  15.             break;
  16. }
  17.         }
  18. }
  19.     return result;
  20. }




点击(此处)折叠或打开

  1. - (UITableViewCell *) tableView:(UITableView *)tableView 
  2.              cellForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4.     UITableViewCell *result = nil;
  5.     if ([tableView isEqual:self.myTableView]){
  6.         static NSString *TableViewCellIdentifier = @"MyCells"; 
  7.         result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
  8.         if (result == nil){
  9.             result = [[UITableViewCell alloc]
  10.                        initWithStyle:UITableViewCellStyleDefault 
  11.                        reuseIdentifier:TableViewCellIdentifier]; 
  12.         }
  13.         result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld",
  14.                         (long)indexPath.section, 
  15.                         (long)indexPath.row];
  16.     }
  17.     return result; 
  18. } 
data source与delegate的区别
data source用于提供数据给table view. 当事件发生时,table view向delegate请求,或者当table view需要更多的信息时。
例如在以下情况下将调用delegate函数
(1)在一个单元被选中或不再选中
(2)当视图需要获得每个单元的高度
(3)当视图需要构建每个section的头部及尾部

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值