UITableView獲取cell的indexPath.row值(多種方式)

一:如果你是自定義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



發佈了45 篇原創文章 · 獲贊 9 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章