按步骤进行记录:
第一步:先进行tableview的创建并且设置代理
第二步:进行注册
第三步:设置组数与宽度和自定义cell的执行次数
第四步:可以通过数组对其进行赋值,将cell中所需要的文字进行传入
第五步:创建继承于uitableviewcell的一个自定义tableviewcell
第六步:在创建的这个cell中添加需要的属性后在.m文件中写方法通过if进行判断
如果等于注册时的那个名字 进行后面的操作 比如上传头像设置字体大小什么的
第七步:通过layousubviews进行位置布局的改变
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
自定义tableviewcell.h中声明几个变量什么的
然后在.m文件中
#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