1.自定義分割線問(wèn)題:
自定義的UITableCell中自定義的分割線(UIView)哨免,在滑動(dòng)時(shí)茎活,cell復(fù)用的時(shí)候,分割線會(huì)消失琢唾。
原因:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//在這里添加的view作為分割線
}
return self;
}
分割線自定義在每個(gè)cell上载荔,當(dāng)cell復(fù)用的時(shí)候,只會(huì)讀取你的數(shù)據(jù)模型的數(shù)據(jù)采桃,但是分割線不會(huì)重新劃線懒熙。
解決辦法:
刪除用view劃線,重寫(xiě)cell的drawRect方法普办,在這里劃線工扎。
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
//上分割線,
//CGContextSetStrokeColorWithColor(context, COLORWHITE.CGColor);
//CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));
//下分割線
CGContextSetStrokeColorWithColor(context,SeparateLineColor.CGColor);
CGContextStrokeRect(context,CGRectMake(10, rect.size.height-1, SCREEN_WIDTH-20,1));
}
2.使用系統(tǒng)分割線:
設(shè)置分割線寬度衔蹲,顏色:
// 和tableView等寬肢娘,也可自定義 UIEdgeInsetsMake(0, 0, 0, 0)
self.tableView.separatorInset = UIEdgeInsetsZero;
self.tableView.separatorColor = [UIColor redColor];