2013-03-29 108 views
57

我如何從單元格獲得indexPathUITableView如何從單元格獲取UITableViewCell indexPath?

我已經搜索了周圍的堆棧溢出和谷歌,但所有的信息是相反的。有沒有辦法訪問superView/UITableView然後搜索單元?有關上下文

的更多信息:有兩類,我有,一個被稱爲Cue和一個被稱爲CueTableCell(這是UITableViewCell的子類)CueTableCellCue視覺表示(這兩個類具有指向彼此)。 Cue對象位於鏈接列表中,並且當用戶執行某個命令時,需要選擇下一個Cue的可視表示(CueTableCell)。因此,Cue類在列表中的下一個Cue上調用select方法,該方法從cell中檢索UITableView並調用其selectRowAtIndexPath:animated:scrollPosition:,爲此需要UITableViewCellindexPath

+2

就像從你的其他[問題](http://stackoverflow.com/questions/15711645/how-to-get-uitableview-from-uitableviewcell)獲取單元格中的「UITableView」 - 爲什麼?這看起來像是一個糟糕的設計決定的代碼味道 - 對於一個單元來說,它沒有太多合法的用例來知道它是否是indexPath,或者它是否在屏幕上。 –

+0

我同意Paul.s的評論。你應該說明你爲什麼要這樣做,因爲你想要做的事可能是一個壞主意。 – rdelmar

+0

@ Paul.s我在問題中發佈了我的程序設計。這可能是不好的編程實踐,在這種情況下,如果你可以提出一個替代方案,那將是非常有用的。一般來說,我對可可非常陌生,對編程也比較陌生。 –

回答

120
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

它可以幫助閱讀documentation,即使這是被一些認爲是有爭議的(見下面的評論)。

小區沒有業務知道它的索引路徑是什麼。 控制器應包含任何基於數據模型操縱UI元素或基於UI交互修改數據的代碼。 (見MVC。)

+30

如果你閱讀這個問題,它會有所幫助。這是來自UITableViewCell,它沒有tableView屬性(並且不是所有帳戶都不應該)。 – voidref

+10

@voidref它也有助於思考這個問題;-)。小區沒有業務知道它的索引路徑是什麼。所以這段代碼應該在控制器中。有關MVC的背景信息,請參閱[Cocoa Core能力](https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html)。 – Mundi

+0

不夠公平,但你的原始回答應該已經解釋了。 – shim

23

這個從你的UITableViewCell嘗試:

NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self]; 

編輯:看來,這並不在iOS 8.1.2和進一步的工作。感謝Chris Prince指出它。

+7

在iOS 8.1.2中,表格視圖單元格的超視圖屬於UITableViewWrapperView類型。該超級視圖是類型表視圖。但我認爲我們正在踩着危險地帶...... –

+1

這一切都可以追溯到「細胞沒有知道它的索引路徑是什麼。」論據。訪問超級視圖幾乎總是一種黑客攻擊,一段時間後它不起作用也就不足爲奇了。 – fatuhoku

+0

完全正確的fatuhoku,它不是知道索引路徑的單元格的事 –

3

這個問題的答案實際上幫了我很多。

我用NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

sender在我的情況是UITableViewCell和來自prepareForSegue方法。

我用這個,因爲我沒有TableViewController,但我不得不UITableView property outlet

我需要找出Cell的標題,因此需要知道它的indexPath。

希望這有助於任何人!

7

爲了解決這些誰說「這是一個壞主意」,在我的情況,我需要這是我對我的UITableViewCell一個按鈕,按下時,是SEGUE另一種觀點。由於這不是電池本身的選擇,所以[self.tableView indexPathForSelectedRow]不起作用。

這讓我兩個選擇:

  1. 商店,我需要傳遞到表格單元本身的視圖對象。雖然這會起作用,但它會擊敗我擁有NSFetchedResultsController的點,因爲我不需要想要將所有對象存儲在內存中,尤其是在表很長的情況下。
  2. 使用索引路徑從提取控制器中檢索項目。是的,我不得不通過黑客來找出NSIndexPath,但它比存儲內存中的對象更便宜。

indexPathForCell:是用正確的方法,但這裏是我會怎麼做(假設這段代碼在UITableViewCell子類來實現:

// uses the indexPathForCell to return the indexPath for itself 
- (NSIndexPath *)getIndexPath { 
    return [[self getTableView] indexPathForCell:self]; 
} 

// retrieve the table view from self 
- (UITableView *)getTableView { 
    // get the superview of this class, note the camel-case V to differentiate 
    // from the class' superview property. 
    UIView *superView = self.superview; 

    /* 
     check to see that *superView != nil* (if it is then we've walked up the 
     entire chain of views without finding a UITableView object) and whether 
     the superView is a UITableView. 
    */ 
    while (superView && ![superView isKindOfClass:[UITableView class]]) { 
     superView = superView.superview; 
    } 

    // if superView != nil, then it means we found the UITableView that contains 
    // the cell. 
    if (superView) { 
     // cast the object and return 
     return (UITableView *)superView; 
    } 

    // we did not find any UITableView 
    return nil; 
} 

PS我真正的代碼做這個訪問的所有從表中的觀點,但我給的,爲什麼有人會想直接做這樣的事情在表格單元格爲例

10
  1. 把弱的tableView財產細胞的.h文件中,如:。

    @property (weak,nonatomic)UITableView *tableView;

  2. 指定在cellForRowAtIndex方法類似物業:

    cell.tableView = tableView;

  3. 現在,只要您需要電池的indexPath:

    NSIndexPath *indexPath = [cell.tableView indexPathForCell:cell];

+0

這是有效的,因爲我們只存儲一個指向tableView本身的指針,但是,indexPathForCell是可靠的嗎?我認爲這隻提供可見單元格的indexPath否? – jcpennypincher

0

這種嘗試從您的UITableViewCell:

NSIndexPath *indexPath = [(UITableView *)self.superview.superview indexPathForCell:self]; 
+0

自ios 8以來無法正常工作 –

1

試試這個(它只能當的tableView只有一個部分,所有的細胞具有高度相等的):

//get current cell rectangle relative to its superview 
CGRect r = [self convertRect:self.frame toView:self.superview]; 

//get cell index 
int index = r.origin.y/r.size.height; 
10

你可以試試這個簡單的提示:

UITableViewCell類:

@property NSInteger myCellIndex; //or add directly your indexPath 

並在您cellForRowAtIndexPath方法:

... 
[cell setMyCellIndex : indexPath.row] 
現在

,您可以檢索任何地方

+2

您需要記住在重新排序或刪除單元格後手動更新此信息。 – konrad

5

你的指數爲SWIFT

let indexPath :NSIndexPath = (self.superview.superview as! UITableView).indexPathForCell(self)! 
0

在iOS上11.0,您可以使用UITableView.indexPathForRow(at point: CGPoint) -> IndexPath?

if let indexPath = tableView.indexPathForRow(at: cell.center) { 
    tableView.selectRow 
    (at: indexPath, animated: true, scrollPosition: .middle) 
} 

它獲取單元格的中心點,然後tableView返回indexPath對應直到那一刻。

相關問題