Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2613787
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-08-24 14:30:44

    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的头部及尾部


 

阅读(399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~