自定义UITableViewCell的两种方式

来源:互联网 发布:linux系统安装ftp包 编辑:程序博客网 时间:2024/05/16 11:11

创建cell比较简单,但是有几个细节点我想说出来,和大家分享一下。使用纯代码创建,先是创建一个cell名字为customCell,继承于UITableViewCell.记着创建的时候不勾选使用XIB这一项,然后就是需要重写- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier这个方法,把自定义的一些item都加到这个方法里边,记着是加到contentView上,而不是加到cell上。我下边一个例子就是创建一个自定义cell,左边是一个label,右边是一个textField。customCell.h文件中,声明两个成员变量,一个label,一个textField,因为不是直接在cell中写死,所以应该是作为cell的属性暴漏在外边,外边可以对其修改。

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@property (nonatomic,strong)UILabel *label;
@property (nonatomic,strong)UITextField *textField;

@end

在.m文件中,我们就是重写tableViewCell的- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier这个方法了,把我们需要的东西都自定义到这个方法里边,是加到cell.contentView上,看我的.m文件:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.label];
        [self.contentView addSubview:self.textField];

    }
    return self;
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc]init];
        _label.frame = CGRectMake(0, 0, 100, 50);
    }
    return _label;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        _textField.frame = CGRectMake(120, 0, 200, 50);
    }
    return _textField;
}

我之前在加label和text的时候是这样加的,出现问题,在cell上根本就显示不出来,原因是这样的:在类的m文件里可以直接用实例变量名来访问自身的实例变量,但是setter和getter方法不会被调用。而外部想用该类的实例变量是需要用getter和setter方法。 所以说要是外部使用了,或者说是修改了等都是需要用self.的
然后就是在controller中这样来写:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"identifier";
    CustomCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.label.text = @"wofr";
    cell.textField.placeholder= @"*******";
    return cell;
}

OK,使用纯代码自定义cell已然完成!!!

下边是通过xib来创建自定义的cell,跟纯代码狠类似,但我感觉xib更简单,是这样的,继承于uitableViewcell,创建的时候记着勾选上使用xib这一项,然后就是在xib中拉几个我们需要的控件,然后对其进行约束,
在创建cell的时候我们需要做:1.设置cell控件的Identfier

2.绑定Cell类并且将控件的输出口关联到TableViewCell.h文件中。

第一步就是设置好我们可能会用到的重用标识符,一般来说都是需要复用的(也有例外,这个自己把握),然后第二部就是设置好我们在用的时候能够确保我们可以找到这个cell,好了,我们在用的时候看下边的controller中的cellForRow 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *cellId = @"identifier";
//    CustomCell *cell;
//    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
//    if (!cell) {
//        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
//    }
//    cell.label.text = @"wofr";
//    cell.textField.placeholder= @"*******";
//    return cell;
    static NSString *cellId = @"tablCell";
    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"TableCell" owner:self options:nil]firstObject];
    }
    cell.textField.placeholder = @"UUUUUUUU";
    cell.label.text = @"w4et4g";
    return cell;
}

记着上边static NSString 定义的那个变量就是我们会用的复用标识符,必须和在XIB中设置的是一样的,这样才可以达到复用的效果,后边的NSBundle mainBundle 中的那个nibname肯定是这个自定义cell的名字,XIB也已经和这个类关联起来了,然后就可以操作了,OK! 完成。。

0 0
原创粉丝点击