2011-04-13 74 views
31

iPad:遍歷UITableView中的每個單元格?iPad:遍歷UITableView中的每個單元格?

+4

無意義的問題,真的:細胞彙集並重用。一般來說,如果你沒有在屏幕上看到它,它就不存在。請詳細說明你正在嘗試做什麼。 – 2011-04-13 05:07:24

+0

+1來評論,你爲什麼需要迭代單元格?通常你會遍歷你填充單元格的底層數據結構。如果由於某種原因需要訪問單元格,可以在'cellForRowAtIndexPath:'中設置基礎數據結構的'cell'屬性以指向初始化的單元格。請確保它是'@property(assign)',但不是'retain',因爲您不想將所有單元格保留在內存中以便重用。 – darvids0n 2011-04-13 05:18:40

+8

這不是沒有意義的; UITableView作爲一些後備數據存儲的可視表示而存在。無論數據存儲的特​​定子集是否在屏幕上,數據存儲都存在。因此,迭代單元格與查詢存儲中的內容相當,即一次一行。 – aroth 2011-04-13 05:19:30

回答

64
for (int section = 0; section < [tableView numberOfSections]; section++) { 
    for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) { 
     NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section]; 
     UITableViewCell* cell = [tableView cellForRowAtIndexPath:cellPath]; 
     //do stuff with 'cell' 
    } 
} 
+5

它只在可見細胞上工作!!!! – 2013-08-23 11:06:54

+5

這是正常的,它只適用於可見細胞。不可見的細胞從記憶中出列; cellForRowAtIndexPath將返回零。 – 2013-09-24 11:08:25

+0

@RaviSharma add [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];它將用於不可見的單元格。 – zaolian 2016-01-25 20:33:31

26

遍歷每一個可見的細胞在一個UITableView:

for (UITableViewCell *cell in self.tableView.visibleCells) { 
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell]; 

(編輯以更好的狀態答案,並希望這是更準確地編入索引的搜索結果與救人更多的時間在未來的意圖)

+3

這只是爲了可見的單元格。 – darvids0n 2011-04-13 05:10:56

+0

我應該說 - 我更新了我的回答以反映您的評論。 – AndrewPK 2011-09-13 15:09:22

+0

對於使用oop風格而不是函數方法的+1,即使您不需要執行此操作,除非您正在更新GUI風格。 – 2013-01-22 13:58:40

2

假設可變myTableView存在並且其委託和數據源均設置:

UITableViewCell *cell; 
NSIndexPath indexPath = [[NSIndexPath indexPathForRow:0 inSection:0]; 
for(indexPath.section = 0; indexPath.section < [myTableView numberOfSections]; ++indexPath.section) 
{ 
    for(indexPath.row = 0; indexPath.row < [myTableView numberOfRowsInSection:indexPath.section]; ++indexPath.row) 
    { 
     cell = [myTableView cellForRowAtIndexPath:indexPath]; 
     // do something with this cell 
    } 
} 
+0

爲indexPath.row獲取這些錯誤..... 1.分配給只讀屬性2.遞增爲只讀屬性.... – iSeeker 2014-02-21 06:01:19

+0

您不能創建新的'NSIndexPath'局部變量。檢查indexPath不是傳入你的tableview數據源函數或類似的東西。 – darvids0n 2014-02-26 01:03:32

4

(此基礎上aroths答案。)

我喜歡把它定義爲一個類別UITableView所以它是隨處可見。

(如提過幾次,你應該確保你真的要遍歷細胞本身,例如:我使用它來將其設置爲選擇的用戶之前清除所有單元UITableViewAccessoryCheckmark的。細胞一個好的經驗法則是要做到這一點只有在數據源的方法不能做你需要什麼)

定義是這樣的:

- (void)enumerateCellsUsingBlock:(void (^)(UITableViewCell *cell))cellBlock { 
    NSParameterAssert(cellBlock != nil); 
    for (int section = 0; section < [self numberOfSections]; section++) { 
     for (int row = 0; row < [self numberOfRowsInSection:section]; row++) { 
      NSIndexPath *cellPath = [NSIndexPath indexPathForRow:row inSection:section]; 
      UITableViewCell *cell = [self cellForRowAtIndexPath:cellPath]; 
      if (cellBlock != nil) { 
       cellBlock(cell); 
      } 
     } 
    } 
} 

呼叫像這樣:

[self.tableView enumerateCellsUsingBlock:^(UITableViewCell *cell) { 
    NSLog(@"cell:%@", cell); 
}]; 

鍵入block也是很好的風格。

1

甚至更​​簡單,更優雅:

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
             forRowAtIndexPath:(NSIndexPath *)indexPath { 
    // use the "cell" here 
} 

但很明顯,它不適合所有的情況。

0

該即時通訊如何遍歷所有表視圖細胞甚至不可見的,在這裏檢查我的答案是:

https://stackoverflow.com/a/32626614/2715840

提示:代碼斯威夫特。

+0

您在OC版本中的解決方案,僅供參考:[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; – zaolian 2016-01-25 20:33:53