[ios development] custom cell

Record in steps:
Step 1: Create a tableview and set up the proxy.
Step 2: Register.
Step 3: Set the number of groups and widths and the number of executions of custom cells.
Step 4: Assign values ​​to them through an array. , Input the text needed in the cell

Step 5: Create a custom tableviewcell inherited from uitableviewcell.
Step 6: Add the required attributes to the created cell and write the method in the .m file.
If it is equal to the name at the time of registration, perform the following operations. For example, upload the avatar to set the font size.
Step 7: Change the position layout through layousubviews

Code implementation of viewcontroller.m

self.mainTableview = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.mainTableView];
    self.mainTableView.dataSource = self;
    self.mainTableView.delegate = self;
    
//    第一种方式注册
    [self.mainTableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"123"];
    [self.mainTableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"1234"];
    
    
    //第二种方式注册 第二种cell比第一种cell创建更加清晰,代码分离度更高。
    //俩种cell写在了不同的类里
    //在这里我解释一下注册
    //我们使用的cell在滑出当前页面的时候都会被放入一个复用池中,这个复用池是栈结构,
    //在滑出下一个cell的时候,会查看服用池,有没有可复用cell,这个判断是根据这个复用标志的,如果有,那么将初始化这个cell的数据(改写),如果没有就重新创建一个cell再初始化。
//    [self.mainTableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"123"];
//    [self.mainTableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"second"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    //第一种方式,所有种类的cell放在一个类里。
    //不必创建大量控件,
    if (indexPath.section %2 == 0) {
    
    
        //第一种
        //比如双数行的cell有一个label、一个imageView
        //单数行有一样的俩个控件只是内容不同,那么创建俩个属性就行了,值在这里对应的每一行去改,可以用数组NSArray[indexpath.section][...]对应内容。
        //如果单数行和双数行控件很大不同,那就要创建俩套控件,值也在这里改。
        //自定义cell里
        MyTableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
        cell.lable.text = @"firstOfDouble";
        return cell;
    } else {
    
    
         MyTableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"1234" forIndexPath:indexPath];
        return cell;
    }
    
    
    
    
    
    
    //第二种方式
    //将不同种cell分开写,也就是单数行对应一个类,双数行对应一个类。
    //这就把需要的控件写成属性即可,值也在这里改。
//    if (indexPath.section %2 == 0) {
    
    
//
//        MyTableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
//        cell.lable.text = @"SecondOfOne";
//
//        return cell;
//    } else {
    
    
//         SecondTableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"1234" forIndexPath:indexPath];
//        cell.lable.text = @"SecondOfDouble";
//        return cell;
//    }
//    
}
@end

Declare several variables in custom tableviewcell.h
and then in the .m file

#import "MyTableViewCell.h"

@implementation MyTableViewCell
//instancetype是clang 3.5开始提供的一个关键字,跟id类似,用于表示某个方法返回的未知类型的Objective-C对象。使用原因:https://www.jianshu.com/p/d2e2e1714b34
//自定义cell不是调用initWithFrame,而是initWithStyle
//reuseIdentifier (标识符)
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if ([self.reuseIdentifier isEqualToString:@"123"]) {
    
    
        _lable = [[UILabel alloc] init];
        [self.contentView addSubview:_lable];
        _lable.text = @"123";
        _tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"temp"]];
        [self.contentView addSubview:_tempImageView];
        
    } else {
    
    
        _lable = [[UILabel alloc] init];
        [self.contentView addSubview:_lable];
        _lable.text = @"1234";
        _tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tempTwo"]];
        [self.contentView addSubview:_tempImageView];
    }
    return self;
}
- (void)layoutSubviews {
    
    
    _lable.frame = CGRectMake(25, 25, 500, 49);
    _tempImageView.frame = CGRectMake(0, 0, 50, 50);
}

@end

Guess you like

Origin blog.csdn.net/m0_46110288/article/details/107625890