我有一個UITableview
與多個單元格。在每個單元格上,我會根據某些數組動態地添加按鈕。所以,當我點擊一個按鈕時,我可以得到該按鈕的值爲tag
。但是如何獲得該細胞的indexPath
?如何在UITableViewCell中獲取UIButton的indexPath?
這是我在-cellForRowAtIndexPath
代碼:
UIView *view=(UIView*)[cell.contentView viewWithTag:indexPath.row+444];
UIImageView *img=(UIImageView*)[cell.contentView viewWithTag:indexPath.row+999];
img.image=[UIImage imageNamed:@"BHCS_empty.png"];
if(integer!=50)
{
NSInteger y_axis=0;
NSArray *Arr=[tableSubCategoryArr objectAtIndex:indexPath.row];
img.image=[UIImage imageNamed:@"BHCS_selected.png"];
view.Frame= CGRectMake(0,50,281, integer-50);
for (int i=0; i<[Arr count]; i++)
{
NSLog(@"arr %@",[Arr objectAtIndex:i]);
UIButton *Btn=[UIButton buttonWithType: UIButtonTypeCustom];
Btn.frame=CGRectMake(0, y_axis, 281, 44);
[Btn setImage:[UIImage imageNamed:@"BHCS_panel.png"] forState:UIControlStateNormal];
[Btn addTarget:self action:@selector(subCategoryBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[Btn setTag:i+100];
[view addSubview:Btn];
UILabel *nameLbl=[[UILabel alloc]initWithFrame:CGRectMake(20, y_axis,248, 44)];
nameLbl.text = [[Arr objectAtIndex:i]objectForKey:@"SubCategoryName"];
nameLbl.textColor = [UIColor whiteColor];
nameLbl.backgroundColor=[UIColor clearColor];
panelTableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BHCS_panel_div1.png"]];
nameLbl.font = [UIFont fontWithName:@"Helvetica" size:12.0f];
[view addSubview:nameLbl];
y_axis=y_axis+44+1.3f;
}
}
一個簡單的方法是創建一個UITableView擴展。 看到我對[這個問題](https://stackoverflow.com/a/39059239/3395921)的回答,看看它是如何做到的。 – 2016-08-20 22:33:24