一個頁面如果用UITableView來展現數據并進行相關操作,可能會經常遇到cell內部按鈕點擊事件,點擊不同的按鈕進行不同的操作或者是展現不同的數據。如下圖所示,點擊右側按鈕可以撥打每個客戶的電話。
由于UITableView的數據是通過其數據源方法來進行展示的,數據通常存儲在模型數組中,要想實現cell內部按鈕點擊獲取到對應cell的信息則可以先獲得當前cell的indexPath或者通過按鈕的TAG值實現。
先來說說前面一種方法,獲取當前cell的indexPath。
不過在此之前,首先要確定你的cell創建方式,是Xib自定義創建的cell還是通過純代碼創建的,因為會有一點區別,如果不提前確定創建cell的方式,也會出現錯誤。
第一種情況:通過XIb創建的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"test";
testCell *cell = (testCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"testCell" owner:self options:nil]lastObject];
}
cell.testLabel.text = self.data[indexPath.row];
//添加按鈕點擊事件
[cell.testBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
//按鈕點擊事件
-(void)click:(UIButton *)btn{
UIView *contentView = [btn superview];
testCell *cell = (testCell *)[contentView superview];
NSIndexPath *indexPath = [self.tb indexPathForCell:cell];
NSLog(@"%@----%@",indexPath,self.data[indexPath.row]);
}
從上往下依次點擊每一行的按鈕,打印結果如下
2017-03-31 16:07:37.077 cell按鈕點擊事件 代理方法[40818:1294020] <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}----第一行
2017-03-31 16:07:38.230 cell按鈕點擊事件 代理方法[40818:1294020] <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}----第二行
2017-03-31 16:07:39.190 cell按鈕點擊事件 代理方法[40818:1294020] <NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}----第三行
2017-03-31 16:07:39.919 cell按鈕點擊事件 代理方法[40818:1294020] <NSIndexPath: 0xc000000000600016> {length = 2, path = 0 - 3}----第四行
第二種情況:純代碼創建的cell
自定義cell的.m文件
#import "customCell.h"
@interface customCell()
@property (nonatomic,strong) UILabel *label;
@end
@implementation customCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(180, 20, 80, 50)];
self.label = label;
[self.contentView addSubview:label];
}
return self;
}
-(void)reloadData:(NSArray *)data index:(NSIndexPath *)index{
self.label.text = data[index.row];
}
在ViewController.m里
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"custom";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[customCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 100, 50)];
[btn setTitle:@"點擊一哈" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//添加按鈕點擊事件
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
[cell reloadData:self.data index:indexPath];
return cell;
}
**使用純代碼創建cell,如果按照前面xib創建cell一樣的方法,在按鈕的點擊事件里代碼如下,運行打印結果則并不是正確的。
-(void)click:(UIButton *)btn{
UIView *contentView = (UIView *)[btn superview];
customCell *cell = (customCell *)[contentView superview];
NSIndexPath *path = [self.TB indexPathForCell:cell];
NSLog(@"%@----%@",indexPath,self.data[indexPath.row]);
}
從上往下一次點擊按鈕,打印結果如下:
2017-03-31 16:21:03.668 customCell[41363:1315437] (null)----第一行
2017-03-31 16:21:05.735 customCell[41363:1315437] (null)----第一行
2017-03-31 16:21:06.535 customCell[41363:1315437] (null)----第一行
2017-03-31 16:21:07.407 customCell[41363:1315437] (null)----第一行
**事實上,通過這種子視圖獲取父視圖,然后再調取- (nullable NSIndexPath )indexPathForCell:(UITableViewCell )cell方法獲得對應cell的NSIndexPath,兩種不同方法創建cell實則存在差異。使用純代碼創建cell的正確方法應該如下
-(void)click:(UIButton *)btn{
customCell *cell = (customCell *)[btn superview];
NSIndexPath *indexPath = [self.TB indexPathForCell:cell];
}
在使用xib創建自定義cell的時候,倘若通過這種方法獲取當前cell的NSIndexPath,可以打開cell的xib視層圖,從按鈕所在的那個圖層依次向上看,遇到一個父視圖就調用superview方法,但是別忘了cell的Content View,再用Content View調用一次superview方法才是你展示在tableview中的cell視圖。
再來后面的一種方法,通過按鈕的Tag值來相應對應按鈕的點擊事件。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"test";
testCell *cell = (testCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"testCell" owner:self options:nil]lastObject];
}
cell.testLabel.text = self.data[indexPath.row];
[cell.testBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//通常為了避免跟系統tag值沖突,一般都會加上一個大數值,調用的時候再減去這個數值
cell.testBtn.tag = indexPath.row + 9999;
return cell;
}
-(void)click:(UIButton *)btn{
NSLog(@"%ld---%@",btn.tag - 9999 ,self.data[btn.tag - 9999]);
}
從上往下一次點擊按鈕,打印結果如下:
2017-03-31 16:45:52.941 cell按鈕點擊事件 代理方法[41664:1326741] 0---第一行
2017-03-31 16:45:53.573 cell按鈕點擊事件 代理方法[41664:1326741] 1---第二行
2017-03-31 16:45:54.556 cell按鈕點擊事件 代理方法[41664:1326741] 2---第三行
2017-03-31 16:45:55.212 cell按鈕點擊事件 代理方法[41664:1326741] 3---第四行
實際上,使用按鈕的TAG值是比較簡單的方法,但是項目中有大量tag值的,并不推薦使用這個方法,容易造成混亂,不好維護。而且通過tag值獲取控件的方式是通過遍歷所有子控件的tag值來完成的,效率會比較低下。
tag有什么壞處呢?目前表現出來的壞處就是,使用太多容易混亂,在復雜度較高的程序中,可讀性很差。間接的也體現了一些工程師,不愛使用enum枚舉的一個陋習,滿篇的xxx.tag = 1....之類的代碼。
tag有什么好處呢?既然UIKit的class里面都有一個tag屬性,肯定是有它存在的道理,tag顧名思義就是給視圖打上標簽,可以用來遍歷子視圖,而不用定義property或是nsarray來進行特定的定義或保存指針。舉個例子,要你生成10個label,是使用循環生成方便還是通過property一個一個定義方便?當然使用循環+局部變量方便的多,如果這10個label同時存在交互,你就有三個選擇:使用tag,或者使用nsarray進行保存指針,或者使用category或繼承來自定義控件實現。這里就很明顯了,使用tag需要的代碼最少,當然也就成了眾多工程師們偷懶的一種方式。