日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

自定义UITableViewCell需注意的问题

發(fā)布時(shí)間:2024/10/12 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义UITableViewCell需注意的问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

自定義cell,有下面幾種方法
方法一:
在controller的.m中

1 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 2 3 @property(nonatomic,strong) UITableView *tableView; 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 self.tableView.delegate = self; 13 self.tableView.dataSource = self; 14 15 ///若cell內(nèi)無數(shù)據(jù),就去除多余cell 16 self.tableView.tableFooterView = [[UIView alloc]init]; 17 18 [self.view addSubview:self.tableView]; 19 } 20 21 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 22 return 10; 23 } 24 25 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 26 static NSString *cellIdentifier = @"myCell"; 27 UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 28 if (myCell == nil) { 29 myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier]; 30 } 31 32 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 20)]; 33 label.text = @"這個(gè)是自定義的cell"; 34 [myCell addSubview:label]; 35 36 return myCell; 37 } 38 39 -(UITableView *)tableView{ 40 if (!_tableView) { 41 _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)]; 42 } 43 return _tableView; 44 } 45 46 -(BOOL)prefersStatusBarHidden{ 47 return YES; 48 } 49 50 @end

這樣可以實(shí)現(xiàn)效果,但若上下滑動(dòng)時(shí),會(huì)有幾行cell字體加粗。如圖:

原因在于cell復(fù)用時(shí),由于每個(gè)服用的cell都會(huì)添加label,所以造成label的重疊。
解決問題的方法就是自定義UITableViewCell

方法二:
如想在cell里添加兩個(gè)button,則需要自定義一個(gè)類,如在.h文件中,button在.h文件中定義

1 @interface ActivieyViewCell : UITableViewCell 2 /** 活動(dòng)內(nèi)容btn */ 3 @property(nonatomic,strong) UIButton *contentBtn; 4 /** 活動(dòng)花費(fèi)btn */ 5 @property(nonatomic,strong) UIButton *costBtn; 6 7 /** 設(shè)置button的位置等信息 */ 8 - (void)setupCellBtn; 9 @end

在.m文件中,直接在自定義方法里定義cell要添加的內(nèi)容

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在復(fù)用時(shí)不會(huì)出現(xiàn)位置混亂等bug.
但上述自定義cell的方法還有一個(gè)bug,還是cell的復(fù)用問題。點(diǎn)擊cell時(shí),背景有時(shí)會(huì)變亂.如圖

原因還是上邊的cell復(fù)用問題。改進(jìn)方法是將setupCellBtn方法寫到initWithStyle中,在初始化時(shí)就設(shè)置。然后在cellForRowAtIndexPath無需使用[actTabCell setupCellBtn];這樣的方法

方法三:
在UITableViewCell的.m中

1 @implementation ActivieyViewCell 2 //重寫該方法,防止復(fù)用時(shí)混亂 3 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 4 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 5 if (self) { 6 [self setupBtnFrame]; 7 } 8 return self; 9 } 10 11 -(void)setupCellBtn{ 12 self.contentBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 13 self.contentBtn.frame = CGRectMake(10, 10, 400, 20); 14 self.contentBtn.backgroundColor = [UIColor brownColor]; 15 [self addSubview:self.contentBtn]; 16 17 self.costBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 18 self.costBtn.frame = CGRectMake(500, 10, 40, 20); 19 self.costBtn.backgroundColor = [UIColor lightGrayColor]; 20 [self addSubview:self.costBtn]; 21 } 22 @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的位置信息等,現(xiàn)在刪除 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 }

這樣就再也不會(huì)出現(xiàn)重疊問題,所以推薦使用方法三,來自定義tableViewCell

?

轉(zhuǎn)載于:https://www.cnblogs.com/Apologize/p/4683558.html

總結(jié)

以上是生活随笔為你收集整理的自定义UITableViewCell需注意的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。