IOS - 自定义表格(UITableView)

来源:互联网 发布:京东云 阿里云 编辑:程序博客网 时间:2024/05/02 02:15

http://blog.csdn.net/zj510/article/details/8444310


在ios开发里面,表格几乎到处被用到。ios的表格控件UITableView是相当的强大,而且很灵活。如果想做出各种效果的table,那么就得使用自定义table了。自定义table也是比较容易的。这里就介绍一下。

首先用xcode创建一个工程,随便什么模板都行,我这里使用了single view模板。

TableView 控件

拖一个Table View控件到xib界面上。如:(这里另外放了个Label,无关紧要的)


然后给这个table增加一个变量(按住control然后拖到对应的.h文件里面),这样在头文件里面就多了一行代码,如:

[cpp] view plaincopy
  1. @property (retain, nonatomic) IBOutlet UITableView *MyTableView;  


相应的UITableViewCell

每个表格的每一行都可以用一个cell来表示,对应的类是UITableViewCell. 为了方便排版,我们可以增加一个xib文件。

右键点击工程,然后选择“new file” -》user interface -》view,点击create,写个文件名:TableViewCell。如图:

打开生成的TableViewCell.xib,将自动生成的view删除,然后再拖一个Table View Cell控件进来,加个Label控件上去,给这个label的tag设置为1.如图:(这个例子只放了一个Label,其实可以放任何东西)

给这个cell增加一个identifier,如图:这里命名为MyTableViewCell


代码实现

使用table,需要实现2个协议:UITableViewDelegate和UITableViewDataSource。UITableViewDelegate需要实现一个函数,如下。这个函数设置行高。

[cpp] view plaincopy
  1. #pragma mark -- table view delegate  
  2.   
  3. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
  4.     return 50;  
  5. }  

UITableViewDataSource需要实现2个函数:

[cpp] view plaincopy
  1. #pragma mark -- DataSource  
  2.   
  3.   
  4. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  5. {  
  6.     return [aryItems count];  
  7. }  
  8.   
  9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  10. {  
  11.     static NSString *CellIdentifier = @"MyTableViewCell";  
  12.       
  13.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  14.     if (cell == nil) {  
  15.         NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil];  
  16.         cell = [nib objectAtIndex:0];  
  17.     }  
  18.       
  19.     if (cell) {  
  20.         UILabel* label = (UILabel*)[cell viewWithTag:1];  
  21.           
  22.         NSString* item = [aryItems objectAtIndex: indexPath.row];  
  23.         [label setText:item];  
  24.     }  
  25.       
  26.       
  27.     return cell;  
  28. }  

其实第一个函数是返回整个表格有多少行。第二个函数就是画每一行(就像windows上画界面的DrawItem)。首先使用identifier来查找是否有空余的已经创建好的cell(比如曾经创建过,但是现在已经滚到上面了,而不需要显示在屏幕中了)。如果没有,那么就使用xib文件创建一个,然后通过tag可以找到这个cell上相应的子控件。如这个例子里面放了一个Label控件,tag是1.得到这个控件后,就可以给这个控件设置内容了。

放一些模拟数据:

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface KViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>  
  4. {  
  5.     NSMutableArray* aryItems;  
  6. }  
  7.   
  8. @property (retain, nonatomic) IBOutlet UITableView *MyTableView;  
  9.   
  10. @end  
头文件里面放了一个数据成员:NSMutableArray。然后给这个array放一些数据,如:

[cpp] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.       
  6.     aryItems = [[NSMutableArray alloc] initWithCapacity:0];  
  7.     [aryItems retain];  
  8.       
  9.     for (int i = 0; i < 10; i++) {  
  10.         NSString* item = [NSString stringWithFormat:@"item %d", i];  
  11.         [aryItems addObject:item];  
  12.     }  
  13.       
  14.     [self.MyTableView setDataSource:self];  
  15.     self.MyTableView.delegate = self;  
  16. }  
放了10行。运行一下看看效果:

哈哈,看到了我们的自定义table。


总结

自定义table其实还是蛮简单的,基本步骤:

1. 拖一个UITableView控件

2. 新建一个xib文件,将原有的view删除,然后拖进去一个UITableViewCell控件,设置Identifier。接着就可以在这个cell上面随意放你想要的子控件了。

3. UITableView对应的class需要实现2个protocol:UITableViewDelegate和UITableViewDataSource。再实现3个基本函数就可以了。


代码:http://download.csdn.net/detail/zj510/4934841 我是用xcode4.5创建的工程(去掉了autolayout)

0 0
原创粉丝点击