71

我怎样才能得到UITableViewCell一个indexPath?像这样:

我使用UIActionSheet,当我单击确认时,UIButton我想更改单元格文本。

我定义为属性的 nowIndex

- (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)
            {

            }
        }
    }
}
4

8 回答 8

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

会给你uitableviewcell。但我不确定你到底要什么!因为你有这段代码,但你仍然在问如何获得 uitableviewcell。更多信息将有助于回答您:)

ADD:这是一种替代语法,可以在没有强制转换的情况下实现相同的目标。

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
于 2010-03-05T04:24:01.793 回答
27

最后,我使用以下代码获取单元格:

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

因为类是扩展的UITableViewController:

@interface SearchHotelViewController : UITableViewController

所以,self就是“SearchHotelViewController”。

于 2010-03-05T08:02:53.680 回答
27

这是从索引路径获取自定义单元格的代码

 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

对于 Swift 4(对于集合视图)

let indexpath = NSIndexPath(row: 2, section: 0)
let cell = self.colVw!.cellForItem(at: indexpath as IndexPath) as? ColViewCell
于 2016-03-23T12:05:08.660 回答
13

对于斯威夫特 4

    let indexPath = IndexPath(row: 0, section: 0)
    let cell = tableView.cellForRow(at: indexPath)
于 2017-11-07T13:12:58.813 回答
7

您可以使用以下代码获取最后一个单元格。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];
于 2015-06-02T12:22:08.663 回答
6

我不太确定您的问题是什么,但是您需要像这样指定参数名称。

-(void) changeCellText:(NSIndexPath *) nowIndex{
    UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] contentView] viewWithTag:contentTag];
    content.text = [formatter stringFromDate:checkInDate.date];
}
于 2010-03-05T03:49:31.903 回答
4

迅速

let indexpath = IndexPath(row: 0, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? <UITableViewCell or CustomCell> {
    cell.backgroundColor = UIColor.red
}
于 2017-07-10T12:45:04.243 回答
4

斯威夫特 3

let indexpath = NSIndexPath(row: 0, section: 0)
let currentCell = tableView.cellForRow(at: indexpath as IndexPath)!
于 2017-06-01T16:46:33.923 回答