附上代码
列表视图创建
- (UITableView *)tableview{
if (!_tableview) {
_tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, SPACING(0.8), SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
_tableview.delegate = self;
_tableview.dataSource = self;
_tableview.backgroundColor = [UIColor clearColor];
_tableview.userInteractionEnabled = YES;
[_tableview registerClass:[PersonInfoTableViewCell class] forCellReuseIdentifier:@"PersonInfoTableViewCell"];
TableViewFootEmpty(_tableview);
}
return _tableview;
}
cell生成代理方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PersonInfoTableViewCell *cell = (PersonInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"PersonInfoTableViewCell"];
if (!cell) {
cell = [[PersonInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInfoTableViewCell"];
}
NSArray *array = self.dataArray[indexPath.section];
[cell configInfo:array[indexPath.row]];
return cell;
}cell自定义
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.userInteractionEnabled = YES;
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.arrow];
[self.contentView addSubview:self.contentLabel];
[self.contentView addSubview:self.headerIcon];
}
return self;
}- (void)layoutSubviews{
[super layoutSubviews];
self.nameLabel.frame = CGRectMake(SPACING(2), (self.height - SPACING(1.5)) / 2., self.width / 4., SPACING(1.5));
self.arrow.frame = CGRectMake(self.width - SPACING(3.), (self.height - SPACING(1.6)) / 2., SPACING(1), SPACING(1.6));
self.contentLabel.frame = CGRectMake(self.nameLabel.rightX + SPACING(1), self.nameLabel.originY, self.arrow.originX - self.nameLabel.rightX - SPACING(2), SPACING(1.7));
self.headerIcon.frame = CGRectMake(self.arrow.originX - SPACING(5), (self.height - SPACING(4)) / 2., SPACING(4), SPACING(4));
self.line.frame = CGRectMake(self.nameLabel.originX, self.height - LINE_HEIGHT, SCREEN_WIDTH - self.nameLabel.originX, LINE_HEIGHT);
if (self.arrow.hidden) {
self.contentLabel.width = self.width - self.nameLabel.rightX - SPACING(3);
}
}
列表视图创建
- (UITableView *)tableview{
if (!_tableview) {
_tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, SPACING(0.8), SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
_tableview.delegate = self;
_tableview.dataSource = self;
_tableview.backgroundColor = [UIColor clearColor];
_tableview.userInteractionEnabled = YES;
[_tableview registerClass:[PersonInfoTableViewCell class] forCellReuseIdentifier:@"PersonInfoTableViewCell"];
TableViewFootEmpty(_tableview);
}
return _tableview;
}
cell生成代理方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PersonInfoTableViewCell *cell = (PersonInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"PersonInfoTableViewCell"];
if (!cell) {
cell = [[PersonInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInfoTableViewCell"];
}
NSArray *array = self.dataArray[indexPath.section];
[cell configInfo:array[indexPath.row]];
return cell;
}cell自定义
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.userInteractionEnabled = YES;
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.arrow];
[self.contentView addSubview:self.contentLabel];
[self.contentView addSubview:self.headerIcon];
}
return self;
}- (void)layoutSubviews{
[super layoutSubviews];
self.nameLabel.frame = CGRectMake(SPACING(2), (self.height - SPACING(1.5)) / 2., self.width / 4., SPACING(1.5));
self.arrow.frame = CGRectMake(self.width - SPACING(3.), (self.height - SPACING(1.6)) / 2., SPACING(1), SPACING(1.6));
self.contentLabel.frame = CGRectMake(self.nameLabel.rightX + SPACING(1), self.nameLabel.originY, self.arrow.originX - self.nameLabel.rightX - SPACING(2), SPACING(1.7));
self.headerIcon.frame = CGRectMake(self.arrow.originX - SPACING(5), (self.height - SPACING(4)) / 2., SPACING(4), SPACING(4));
self.line.frame = CGRectMake(self.nameLabel.originX, self.height - LINE_HEIGHT, SCREEN_WIDTH - self.nameLabel.originX, LINE_HEIGHT);
if (self.arrow.hidden) {
self.contentLabel.width = self.width - self.nameLabel.rightX - SPACING(3);
}
}
既然是用纯代码写的,这年头要么xib,要么stroyboard混着代码用,可以大幅度减少代码量
我看你这纯代码写的 似乎也还没有封装啊!
你用当前视图创建的控件,然后用cell去获取他们,虽然也有这种写法,但是非常不推荐
建议你先修改你这种写代码的习惯!
或许你认为的bug可能就自动消失了
1、table的代理你这没贴出来不知道写了没有,就是定义table多少行多少列,不过一般都会定义
2、把cell定义的内容写到PersonInfoTableViewCell 这个类里面去
3、可以使用storyboard或者xib 起码可以减少80%的代码量