代码实现 UITableView与UITableViewCell

我们常用的表格类视图就是用 UITableView与UITableViewCell,UITableViewController继承UIViewContoller,所以只要很少代码就可以显示一个视图,UITableViewController也是UIScrollView子类,所以也有上下滑动效果 ;UITableView和UITableViewCell不能储存数据,可以用来显示特定行数内的数据,而且,也并不是把所有数据都放在单元格cell视图上,而是通过单元格重用和实现UITableViewDataSource,UITableViewDelegate协议的方法形式显示出来;


1.新建工程名为SampleTable , File->New->Project ->single View Application -> next 



2.添加UITableViewDataSource,UITableViewDelegate协议

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
  4.   
  5. @property(strong,nonatomic) NSArray *listData;  
  6. @property(strong,nonatomic)UITableView *tableView;  
  7. @property(strong,nonatomic)UITableViewCell *tableViewCell;  
  8.   
  9. @end  
声明了一个存放数据的数组和用于显示单元格的两个对象


2.在@implementation STViewController后面添加上

@synthesize listData=_listData;

@synthesize tableView = _tableView;

@synthesize tableViewCell =_tableViewCell

viewDidLoad中实现对界面初始化工作,UITableView有两种风格,

UITableViewStylePlain    默认风格,最常见的

UITableViewStyleGrouped  圆角矩形风格


[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.     //初始化表格  
  6.     self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];  
  7. // 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作     
  8.     self.tableView.delegate=self;  
  9.     self.tableView.dataSource=self;  
  10.     //把tabView添加到视图之上  
  11.     [self.view addSubview:self.tableView];     
  12. //    存放显示在单元格上的数据  
  13.     NSArray *array = [NSArray arrayWithObjects:@"张三",@"张四",@"张五",@"李三",@"李四",@"李五",@"李六",@"王三",@"王四",@"王五",@"王六",@"王七",@"王八",@"王九",@"王十", nil];  
  14.     self.listData = array;  
  15.       
  16. }  

3.视图上显示单元格的内容以及一些数据都是都是属性都是依赖于协议的代理方法

[cpp]  view plain copy
  1. //返回多少个section  
  2. -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView  
  3. {  
  4.     return 1;  
  5. }  
[cpp]  view plain copy
  1. //返回行数,也就是返回数组中所存储数据,也就是section的元素  
  2. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  3. {  
  4.     return [self.listData count];  
  5. }  

UITableView每一行都有一个UITableViewCell的实例表示,它也继承UIView,也就是每一行又拥有一个子视图,如果是大型表格,这样开销就非常大,所以就有了单元格的重用;当一部分单元格滚出屏幕后,他们被放在一个可重用的单元序列之中。如果系统运行比较慢,表视图就会从序列中删除这些单元,释放空间,如果有储存空间,表视图就会重新获取这些单元,以后面使用;

[cpp]  view plain copy
  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. //    声明静态字符串型对象,用来标记重用单元格  
  4.     static NSString *TableSampleIdentifier = @"TableSampleIdentifier";  
  5. //    用TableSampleIdentifier表示需要重用的单元  
  6.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];  
  7. //    如果如果没有多余单元,则需要创建新的单元  
  8.     if (cell == nil) {  
  9.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];  
  10.     }  
  11.       
  12.     else {  
  13.         while ([cell.contentView.subviews lastObject ]!=nil) {  
  14.             [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];  
  15.         }  
  16.     }  
  17. //    获取当前行信息值  
  18.     NSUInteger row = [indexPath row];  
  19. //    填充行的详细内容  
  20.     cell.detailTextLabel.text = @"详细内容";  
  21. //    把数组中的值赋给单元格显示出来  
  22.     cell.textLabel.text=[self.listData objectAtIndex:row];  
  23.       
  24.       
  25. //    cell.textLabel.backgroundColor= [UIColor greenColor];  
  26.       
  27. //    表视图单元提供的UILabel属性,设置字体大小  
  28.     cell.textLabel.font = [UIFont boldSystemFontOfSize:40.0f];  
  29. //    tableView.editing=YES;  
  30. /* 
  31.     cell.textLabel.backgroundColor = [UIColor clearColor]; 
  32.         UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame]; 
  33.     backgroundView.backgroundColor = [UIColor greenColor]; 
  34.     cell.backgroundView=backgroundView; 
  35.    */   
  36. //    设置单元格UILabel属性背景颜色  
  37.     cell.textLabel.backgroundColor=[UIColor clearColor];  
  38. //    正常情况下现实的图片  
  39.     UIImage *image = [UIImage imageNamed:@"2.png"];  
  40.     cell.imageView.image=image;  
  41.       
  42. //    被选中后高亮显示的照片  
  43.     UIImage *highLightImage = [UIImage imageNamed:@"1.png"];  
  44.     cell.imageView.highlightedImage = highLightImage;  
  45.     return cell;  
  46. }  
注释内容中有这两个设置表视图背景颜色的属性方法,具体了解可以看这个博客 http://haoxiang.org/2010/12/uitableviewcell-background/   讲的比较详细
[cpp]  view plain copy
  1. cell.textLabel.backgroundColor= [UIColor greenColor];  
[cpp]  view plain copy
  1. cell.textLabel.backgroundColor = [UIColor clearColor];  
  2.     UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];  
  3. backgroundView.backgroundColor = [UIColor greenColor];  
  4. cell.backgroundView=backgroundView;  

表示UITableViewCell风格的常量有:

UITableViewCellStyleDefault

UITableViewCellStyleSubtile

UITableViewCellStyleValue1

UITableViewCellStyleValue2




[cpp]  view plain copy
  1. //设置单元格高度  
  2. -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     return 90;  
  5.   
  6. }  
[cpp]  view plain copy
  1. //设置单元格缩进  
  2. -(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     NSInteger row = [indexPath row];  
  5.     if (row % 2==0) {  
  6.         return 0;  
  7.     }  
  8.     return 2;  
  9. }  
[cpp]  view plain copy
  1. //选中单元格所产生事件  
  2. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4. //    首先是用indexPath获取当前行的内容  
  5.     NSInteger row = [indexPath row];  
  6. //    从数组中取出当前行内容  
  7.     NSString *rowValue = [self.listData objectAtIndex:row];  
  8.     NSString *message = [[NSString alloc]initWithFormat:@"You selected%@",rowValue];  
  9. //    弹出警告信息  
  10.     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"  
  11.                                                    message:message  
  12.                                                   delegate:self  
  13.                                          cancelButtonTitle:@"OK"  
  14.                                          otherButtonTitles: nil];  
  15.     [alert show];  
  16. }  


 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

                  这个方法返回指定的 section的header view 的高度。

  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

                  这个方法返回指定的 section的footer view 的高度。



为了增加效果,所以界面显得比较丑陋,附上运行结果截图





附上源代码:http://download.csdn.net/detail/duxinfeng2010/4416166

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值