IOS - 自定义表格(UITableView)

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

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

TableView 控件

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


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

@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需要实现一个函数,如下。这个函数设置行高。

#pragma mark -- table view delegate

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

UITableViewDataSource需要实现2个函数:

#pragma mark -- DataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [aryItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyTableViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    if (cell) {
        UILabel* label = (UILabel*)[cell viewWithTag:1];
        
        NSString* item = [aryItems objectAtIndex: indexPath.row];
        [label setText:item];
    }
    
    
    return cell;
}

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

放一些模拟数据:

#import <UIKit/UIKit.h>

@interface KViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray* aryItems;
}

@property (retain, nonatomic) IBOutlet UITableView *MyTableView;

@end
头文件里面放了一个数据成员:NSMutableArray。然后给这个array放一些数据,如:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    aryItems = [[NSMutableArray alloc] initWithCapacity:0];
    [aryItems retain];
    
    for (int i = 0; i < 10; i++) {
        NSString* item = [NSString stringWithFormat:@"item %d", i];
        [aryItems addObject:item];
    }
    
    [self.MyTableView setDataSource:self];
    self.MyTableView.delegate = self;
}
放了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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值