[ios]tablbviewcell

本文详细介绍使用Storyboard创建TableView及自定义TableViewCell的过程,并提供纯代码方式自定义TableViewCell的方法。包括如何在Storyboard中设置TableView和TableViewCell,如何通过代码指定Cell的高度和内容等。

1.storyboard创建tableview,拉进controller中建立链接

 

2.storyboard建立约束:在连接器上将dataSource delegate拉到controller view的小按钮上

在controller.h上继承UIViewController<UITableViewDataSource,UITableViewDelegarte>

 

3.在controller.m中添加方法:

numberOfRowsInSection(注意这里counts是否真的有值)

cellForRowAtIndewPat

 

4.添加tableViewCell:

(1)在storyboard的tableview上拉上tableViewCell,设置好样式

 (2)建一个tableviewcell的类,把样式的控件链接上.h

(3)storyboard上Custom Class改变指向类,Identifier上命名识别名称

 

5.在代码中指向cell:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIden = @"deviceListCell";
    
    DeviceListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden forIndexPath:indexPath];
    
    cell.deviceName.text = @"test";//for test

    return cell;
}

 

6.调整cell的高度

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

 

二.纯代码自定义cell

参考:http://blog.youkuaiyun.com/u012350430/article/details/51181728

重写-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier:

 

//cell自定义用的是-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //这里顺便介绍小UIButton的创建
        //设置button的类型是UIButtonTypeRoundedRect
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        //设置button的frame
        button.frame = CGRectMake(20, 20, 50, 50);

        //button正常状态title设置为Yes,被选择状态title设置为No
        [button setTitle:@"Yes" forState:UIControlStateNormal];
        [button setTitle:@"No" forState:UIControlStateSelected];

        //设置button响应点击事件的方法是buttonPressed:
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        //添加到cell
        [self addSubview:button];

        //创建imageView添加到cell中
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Totoro副本"]];
        imageView.frame = CGRectMake(150, 20, 150, 100);
        [self addSubview:imageView];

    }
    return self;
}

//buttonPressed:方法
-(void)buttonPressed:(UIButton *)button
{
    //实现按钮状态的切换
    button.selected = !button.selected;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值