2010-03-05 171 views
62

如何通過indexPath獲得UITableViewCell?像這樣:如何通過indexPath獲取uitableViewCell?

我使用UIActionSheet,當我點擊確認UIButton我想更改單元格文本。

我定義爲一個屬性

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     NSInteger section = [nowIndex section]; 
     NSInteger row = [nowIndex row]; 
     NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
     formatter.dateFormat = @"yyyy-MM-dd"; 

     if (section == 0) 
     { 
      if (row == 0) 
      { 

      } 
      else if (row == 1) 
      { 
       UILabel *content = (UILabel *)[[(UITableView *)[actionSheet ] cellForRowAtIndexPath:nowIndex] viewWithTag:contentTag]; 
       content.text = [formatter stringFromDate:checkInDate.date]; 
      } 
      else if (row == 2) 
      { 

      } 
     } 
    } 
} 

回答

5

的nowIndex我不太清楚你的問題是什麼,但你需要指定像這樣的參數名稱。

-(void) changeCellText:(NSIndexPath *) nowIndex{ 
    UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] contentView] viewWithTag:contentTag]; 
    content.text = [formatter stringFromDate:checkInDate.date]; 
} 
+0

喜大衛 我改寫了問題,你能看到它再次 – phpxiaoxin 2010-03-05 04:05:40

111
[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] 

會給你的UITableViewCell。但我不確定你到底在問什麼!因爲你有這個代碼,你仍然在問如何獲得uitableviewcell。一些更多的信息將有助於回答你:)

地址:這是另一種語法,實現沒有演員相同的事情。

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex]; 
+1

*技術上*要調用上無論您的tableView的'dataSource'屬性設置爲,但通常是'self'。 – devios1 2013-05-08 21:36:13

26

最後,我用下面的代碼獲取細胞:

UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex]; 

由於類擴展UITableViewController:

@interface SearchHotelViewController : UITableViewController 

所以,self是 「SearchHotelViewController」。

+1

命名一個類(..)如果它是UIViewController的子類,View就會令人困惑。 – johnyu 2013-12-11 13:29:44

6

您可以使用下面的代碼來獲取最後一個單元格。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath]; 
14

下面是一個代碼從指數路徑獲取自定義單元格

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0]; 
YourCell *cell = (YourCell *)[tblRegister cellForRowAtIndexPath:indexPath]; 

對於斯威夫特

let indexpath = NSIndexPath(forRow: 2, inSection: 0) 
let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! CellTest 
+0

在有兩個自定義單元格的情況下,我需要知道在此索引路徑上返回哪種類型的單元格。什麼是最好的方法來獲得這種類型? – 2017-06-26 18:04:47

2

斯威夫特3

let indexpath = NSIndexPath(row: 0, section: 0) 
let currentCell = tableView.cellForRow(at: indexpath as IndexPath)! 
3

斯威夫特

let indexpath = IndexPath(row: 0, section: 0) 
if let cell = tableView.cellForRow(at: indexPath) as? <UITableViewCell or CustomCell> { 
    cell.backgroundColor = UIColor.red 
} 
4

對於斯威夫特4

let indexPath = IndexPath(row: 0, section: 0) 
    let cell = tableView.cellForRow(at: indexPath) 
相關問題