一:如果你是自定義cell,新建 .xib,中的按鈕爲:AotuBtn
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//自定義代碼
}
兩種方式:
//獲取點擊cell的indexPath第一種方式
UITableViewCell *cell = (UITableViewCell *)[[[sender superview] superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"indexPath is = %li",(long)indexPath.row);
//獲取點擊cell的indexPath第二種方式
UITableViewCell *cell = (UITableViewCell *)[[[sender superview] superview] superview];
CGPoint hitPoint = [cell convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
NSLog(@"indexPath is = %li",(long)hitIndex.row);
二:如果你是用代碼方式直接將控件(如UILabel、UIButton等)加到UITableView的cell中
UITableViewCell *cell = (UITableViewCell *)[btn superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"indexPath is = %i",indexPath.row);
其他相關文章:
獲取tableView(UICollectionViewCell)中cell相對於(UICollectionView)tableView.superView的區域座標(位置)
http://blog.csdn.net/www9500net_/article/details/52437987