你好,游客 登录 注册 搜索
背景:
阅读新闻

iOS学习之Table View的简单使用

[日期:2012-06-14] 来源:csdn  作者:totogo2010 [字体:大 中 小]
7、生成row
关键的步骤来了,实现tableview添加数据源,返回TableView的行数,返回各行cell实例。
  1. - (UITableViewCell *)tableView:(UITableView *)tableView   
  2.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
  3.       
  4.     static NSString *TableSampleIdentifier = @"TableSampleIdentifier";   
  5.       
  6.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   
  7.                              TableSampleIdentifier];   
  8.     if (cell == nil) {   
  9.         cell = [[UITableViewCell alloc]   
  10.                 initWithStyle:UITableViewCellStyleDefault   
  11.                 reuseIdentifier:TableSampleIdentifier];   
  12.     }   
  13.       
  14.     NSUInteger row = [indexPath row];   
  15.     cell.textLabel.text = [self.list objectAtIndex:row];   
  16.     return cell;   
  17. }  
上面的第二个方法中,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。
这里UITableViewCellStyleDefault是表示UITableViewCell风格的常数,除此之外,还有其他风格,后面将会用到。
注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath row];获取行号。之后给cell设置其文本:
cell.textLabel.text = [self.list objectAtIndex: row];

8、现在一个简单的TableView就弄好看,运行下看效果
、、

9、添加图片。
在项目上add files到项目,提交两张小图片,然后在cell返回之前添加如下代码
  1. NSUInteger row = [indexPath row];   
  2.     cell.textLabel.text = [self.list objectAtIndex:row];   
  3.     UIImage *image = [UIImage imageNamed:@"qq"];   
  4.     cell.imageView.image = image;   
  5.     UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];   
  6.     cell.imageView.highlightedImage = highLighedImage;  
  7.     return cell;   
效果如下:
 
linux
相关资讯       iOS编程开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款