您有很多方法来获取单元格或单元格的 indexPath:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect
- (NSArray *)visibleCells
- (NSArray *)indexPathsForVisibleRows
如果你想处理 UIButton 的 IBAction 而不是像这样使用 indexPathForRowAtPoint :
-(IBAction)myAction:(id)sender
{
CGPoint location = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipeCell = [self.tableView cellForRowAtIndexPath:indexPath];
NSLog(@"Selected row: %d", indexPath.row);
//......
}
或 indexPathForCell
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
// Get the UITableViewCell which is the superview of the UITableViewCellContentView which is the superview of the UIButton
(UITableViewCell*)cell = [[button superview] superview];
int row = [myTable indexPathForCell:cell].row;
}
我从以下代码中获取了代码:
Detecting which UIButton was pressed in a UITableView
自定义 UITableViewCell 按钮操作?