2010-03-05 170 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) 
相关问题