如何通过indexPath获取uitableViewCell?

php*_*xin 68 iphone uitableview nsindexpath

我怎样才能获得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)
            {

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Man*_*ath 116

[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex]
Run Code Online (Sandbox Code Playgroud)

会给你uitableviewcell.但我不确定你究竟要求的是什么!因为你有这个代码,但你仍然在问如何获得uitableviewcell.更多信息将有助于回答你:)

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

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
Run Code Online (Sandbox Code Playgroud)


php*_*xin 27

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

UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex];
Run Code Online (Sandbox Code Playgroud)

因为课程是扩展的 UITableViewController:

@interface SearchHotelViewController : UITableViewController
Run Code Online (Sandbox Code Playgroud)

所以,self是"SearchHotelViewController".


Har*_*kar 23

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

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
 YourCell *cell = (YourCell *)[tblRegister cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

对于斯威夫特

let indexpath = NSIndexPath(forRow: 2, inSection: 0)
let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! CellTest
Run Code Online (Sandbox Code Playgroud)

适用于Swift 4(适用于collectionview)

let indexpath = NSIndexPath(row: 2, section: 0)
let cell = self.colVw!.cellForItem(at: indexpath as IndexPath) as? ColViewCell
Run Code Online (Sandbox Code Playgroud)


小智 11

对于Swift 4

    let indexPath = IndexPath(row: 0, section: 0)
    let cell = tableView.cellForRow(at: indexPath)
Run Code Online (Sandbox Code Playgroud)


Dav*_*rek 6

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

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


Har*_*ora 6

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];
Run Code Online (Sandbox Code Playgroud)