自定义UITableViewCell需注意的问题
自定義cell,有下面幾種方法
方法一:
在controller的.m中
這樣可以實現效果,但若上下滑動時,會有幾行cell字體加粗。如圖:
原因在于cell復用時,由于每個服用的cell都會添加label,所以造成label的重疊。
解決問題的方法就是自定義UITableViewCell
方法二:
如想在cell里添加兩個button,則需要自定義一個類,如在.h文件中,button在.h文件中定義
在.m文件中,直接在自定義方法里定義cell要添加的內容
1 @implementation ActivieyViewCell 2 -(void)setupCellBtn{ 3 self.contentBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 4 self.contentBtn.frame = CGRectMake(10, 10, 400, 20); 5 self.contentBtn.backgroundColor = [UIColor brownColor]; 6 [self addSubview:self.contentBtn]; 7 8 self.costBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 9 self.costBtn.frame = CGRectMake(500, 10, 40, 20); 10 self.costBtn.backgroundColor = [UIColor lightGrayColor]; 11 [self addSubview:self.costBtn]; 12 } 13 @end在tableView的cellForRowAtIndexPath中添加
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 2 static NSString *cellIdentify = @"activityList"; 3 //在該處就可以使用自定義的tableViewCell 4 //前邊的都是用 5 //UITableViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 6 ActivieyViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 7 8 if (actTabCell==nil) { 9 actTabCell = [[ActivieyViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]; 10 } 11 12 //在該處定義button的位置信息等 13 [actTabCell setupCellBtn]; 14 15 [actTabCell.contentBtn setTitle:actModel.activityContent forState:UIControlStateNormal]; 16 [actTabCell.contentBtn setBackgroundColor:[UIColor brownColor]]; 17 actTabCell.contentBtn.titleLabel.numberOfLines = 0; 18 19 [actTabCell.costBtn setTitle:actModel.activityCost forState:UIControlStateNormal]; 20 [actTabCell.costBtn setBackgroundColor:[UIColor lightGrayColor]]; 21 actTabCell.costBtn.titleLabel.numberOfLines = 0; 22 23 return actTabCell; 24 }這樣寫的好處就是tableViewCell在復用時不會出現位置混亂等bug.
但上述自定義cell的方法還有一個bug,還是cell的復用問題。點擊cell時,背景有時會變亂.如圖
原因還是上邊的cell復用問題。改進方法是將setupCellBtn方法寫到initWithStyle中,在初始化時就設置。然后在cellForRowAtIndexPath無需使用[actTabCell setupCellBtn];這樣的方法
方法三:
在UITableViewCell的.m中
然后在tableView的cellForRowAtIndexPath中添加
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 2 static NSString *cellIdentify = @"activityList"; 3 //在該處就可以使用自定義的tableViewCell 4 //前邊的都是用 5 //UITableViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 6 ActivieyViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 7 8 if (actTabCell==nil) { 9 actTabCell = [[ActivieyViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]; 10 } 11 12 //原來是在該處定義button的位置信息等,現在刪除 13 //[actTabCell setupCellBtn]; 14 15 [actTabCell.contentBtn setTitle:actModel.activityContent forState:UIControlStateNormal]; 16 [actTabCell.contentBtn setBackgroundColor:[UIColor brownColor]]; 17 actTabCell.contentBtn.titleLabel.numberOfLines = 0; 18 19 [actTabCell.costBtn setTitle:actModel.activityCost forState:UIControlStateNormal]; 20 [actTabCell.costBtn setBackgroundColor:[UIColor lightGrayColor]]; 21 actTabCell.costBtn.titleLabel.numberOfLines = 0; 22 23 return actTabCell; 24 }這樣就再也不會出現重疊問題,所以推薦使用方法三,來自定義tableViewCell
?
轉載于:https://www.cnblogs.com/Apologize/p/4683558.html
總結
以上是生活随笔為你收集整理的自定义UITableViewCell需注意的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一次开通花呗的额度是多少
- 下一篇: 100元学生保险报销范围