ios UITableView

UITableView是一中可编辑的、可拖动的UI控件。他的用处非常大,比如我们手机的联系人列表,QQ上的联系人列表,音乐播放器的歌名列表等。几乎每一个软件都要用到TableView。下面我们就来看一下TableView是如何定义的:

在定义TableView之前,首先要遵循他的两个协议,一个是UITableViewDelegete(UITableView的代理),另一个是UITableViewDatasource(UITableView的数据源):

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

做完这个步骤以后,我们需要再定义一个可变数组,用来存储TableView里面要显示的数据。

然后我们就可以开始定义TableView了:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.title = @"UITableView";
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    [_table setDelegate:self];//遵循uitableview协议
    [_table setDataSource:self];//遵循uitableview数据源协议
    [self.view addSubview:_table];
}

这里TableView的样式有两个:一种是UITableViewStylePlain,另一种是UITableViewStyleGrouped。

定义完以后,我们会发现系统会出现警告,这是因为我们有两个必须实现的方法还没有实现:

//设置tableview每一个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}

//建立uitableviewcell并显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellindentify = @"cell";
    //uitableviewcell的重用机制
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentify];
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellindentify] autorelease];
    }
    [cell.textLabel setText:[_arr objectAtIndex:indexPath.row]];//给cell设置文字
    [cell.detailTextLabel setText:[_arr objectAtIndex:(_arr.count - 1) - indexPath.row]];
    return cell;
}

这里我们可能对UITableViewCell这个东西比较陌生,这里的cell就是TableView上显示的每一行。cell上有三个元素,分别是TextLabel、DetailTextLabel和ImageView,他们都是非必须显示的。这里我们定义完以后,来显示一下:


cell有四种样式,分别是UITableViewCellStyleDefault,UITableViewCellStyleSubtitle,UITableViewCellStyleValue1,UITableViewCellStyleVlaue2.这里我用的是Value1的样式,剩下三种有兴趣可以试一下。

我们都知道,View可以自定义,那么cell可不可以自定义呢,答案是肯定的。在我们的项目过程中,系统默认的样式已经远远满足不了我们的要求,我们需要自已定义cell:

首先,新建一个文件,继承于UITableViewCell:


我们需要在.h文件里先对两个成员声明成属性:

@interface TableViewCell : UITableViewCell

@property (nonatomic,retain) UILabel *name1;
@property (nonatomic,retain) UILabel *name2;

@end

在.m文件里对这两个成员进行实现:

#import "TableViewCell.h"

@implementation TableViewCell
- (void)dealloc
{
    [_name1 release];
    [_name2 release];
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        _name1 = [[UILabel alloc]initWithFrame:CGRectZero];
        _name2 = [[UILabel alloc]initWithFrame:CGRectZero];
        [self.contentView addSubview:_name1];
        [self.contentView addSubview:_name2];
 }
    return self;
}
//这个方法每次cell将要显示的时候就会调用这个方法,我们一般在这里给成员设置位置。
- (void)layoutSubviews{
    [_name1 setFrame:CGRectMake(5, 5, 60, 30)];
    [_name2 setFrame:CGRectMake(70, 5, 60, 30)];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

这样,自定义cell就完成了。下面我们开始调用:

我们把刚才实现cell的方法改动一下,让cell变成我们自定义cell的对象:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellindentify = @"cell";
    //uitableviewcell的重用机制
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentify];
    if (!cell) {
        cell = [[[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellindentify] autorelease];
    }
    [cell.name1 setText:[_arr objectAtIndex:indexPath.row]];//给cell设置文字
    [cell.name2 setText:[_arr objectAtIndex:(_arr.count - 1) - indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

这里我们多加了一个方法,设置cell的行高,下面我们来运行一下:

这样我们就可以实现各种各样的cell。在tableview上实现的移动、添加、和删除功能,我会在下一期讲到,敬请期待。

欢迎留言执政错误,感激不尽。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值