继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

iOS学习笔记--UITableView (三)

ZKReadStone
关注TA
已关注
手记 52
粉丝 32
获赞 322

UITableView笔记三

2016-03-31 11:47:39

UITableView使用Xib的注意事项

<!---more--->

创建自定义cell

MyGif

设置cell Xib的标识

MyGif

如果为了代码的可读性也可以像#define kTestTableViewCellHeight 60.0f一样把cell的唯一标识也写成宏定义 放在相同的地方
使用的时候就变成,简洁明了。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTestTableViewCellId forIndexPath:indexPath];
    cell.titleLabel.text = self.dataArray[indexPath.row];

    return cell;
}

#pragma mark tableView Getter
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        [_tableView registerNib:[UINib nibWithNibName:@"TestTableViewCell" bundle:nil] forCellReuseIdentifier:kTestTableViewCellId];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = kTestTableViewCellHeight;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
在TableView中的注册和使用

MyGif

代码同上

Xib的简单约束和cell上按钮点击回调的简单使用

MyGif

并附上cell里面的代码:

.h

#import <UIKit/UIKit.h>

#define kTestTableViewCellHeight 60.0f
#define kTestTableViewCellId @"TestTableViewCell"

typedef void(^BtnBlock)();

@interface TestTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (nonatomic, copy)BtnBlock btnBlock;

@end

.m

#import "TestTableViewCell.h"

@implementation TestTableViewCell

- (void)awakeFromNib
{

}

- (IBAction)btnClicked:(id)sender
{
    self.btnBlock();
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end

使用的时候:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTestTableViewCellId forIndexPath:indexPath];
    cell.titleLabel.text = self.dataArray[indexPath.row];

    cell.btnBlock = ^{

    ///这里做点击按钮的相关操作。

    };
    return cell;
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP