如何获得UITableViewCell中的UIButton的indexPath?
我有一个UITableview
与多个单元格。 在每个单元格上,我都根据一些数组dynamic地添加button。 所以,当我点击一个button,我可以得到button的tag
值。 但如何获得该单元格的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; } }
我已经尝试了最多的给定的答案,但在和我通常使用去最广义和理想的方式如下:
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
ALLLLL的答案在这里是不好的,你不应该通过superviews循环。 经典的例子,与iOS 7苹果改变了tableViewCell
层次结构,你的应用程序现在将崩溃!
用这个代替:
如何知道UITableview行号
感谢所有我解决这个通过使用下面的代码
NSIndexPath *indexPath = [panelTableView indexPathForCell:(UITableViewCell *)sender.superview.superview]; NSLog(@"%d",indexPath.row);
更新了答案
像这样使用它:
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
这些答案都不是一个非常干净的解决scheme。 我将实现这一点的方式是使用委托模式。 视图控制器是单元格的委托,这意味着你让单元格本身处理button的按下,它告诉它的委托,当button被按下,所以它可以处理它,但是它想要的。
假设你有一个tableview,每个单元格代表一个Person
对象,当button被按下时,你想显示这个人的个人资料。 所有你需要做的是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath]; cell.delegate = self; cell.person = self.people[indexPath.row]; return cell; } - (void)personCell:(PersonCell *)personCell didPressButtonForPerson:(Person *)person { [self showProfileForPerson:person]; }
然后,你需要在你的button类中添加一个名为buttonPressedHandler
的属性,这个属性是一个传回Person
实例的块,当你创buildbutton并添加目标时,可以这样做:
- (void)viewDidLoad { [super viewDidLoad]; // do whatever whatever else you need/want to here [self.button addTarget:self selector:@selector(handleButtonPressed) forState:UIControlStateNormal]; } - (void)handleButtonPressed { // Make sure this is a required delegate method, or you check that your delegate responds to this selector first... [self.delegate personCell:self didPressButtonForPerson:self.person]; }
把这个代码放在button的操作方法中:
NSIndexPath *indexPath = [yourtableviewname indexPathForCell:(UITableViewCell *)sender.superview]; NSLog(@"%d",indexPath.row);
我build议添加一个属性到你的自定义UITableViewCell实现类来存储indexPath。 然后,当您的CellForRowAtIndexPath委托在您的TableViewController触发时,设置该单元格的indexPath属性。
customTableViewCell.h:
@interface customTableViewCell : UITableViewCell @property NSIndexPath *indexPath; @end
customTableViewControllerconfiguration单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { customTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath]; // Do whatever you want with your cell cell.indexPath = indexPath; return cell; }
然后,您可以通过调用self.indexPath来引用customTableViewCell中的indexPath属性
你可以像这样使用UITableView
的indexPathForCell:
方法[tableView indexPathForCell:cell];
。 祝你好运!
- Firebase Swift:childKey的queryEqualToValue不起作用
- JSONparsingswift,数组在NSURLSession之外没有任何值
- 在swift 2中无法使用AVCaptureVideoDataOutput通过CGDataProviderCopyData获取像素数据
- 在UITextView中查找可见文本的范围
- 如何更新ios中的textfield值
- 将数据从View Controller传递到Swift中的子控制器
- 使用Swift在React-Native中基于Promise的本机模块
- 在Button上单击更改viewController
- 按下播放后,UIWebView上的QuickTime播放器iOS 9会发出警告