ChatGPT解决这个技术问题 Extra ChatGPT

如何通过 indexPath 获取 uitableViewCell?

我怎样才能通过 indexPath 获得 UITableViewCell?像这样:

我使用 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)
            {

            }
        }
    }
}

K
Kent
[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex]

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

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

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

技术上你想在你的 tableView 的 dataSource 属性设置的任何地方调用它,但通常是 self
R
Rui Peres

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

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

因为类扩展了UITableViewController:

@interface SearchHotelViewController : UITableViewController

因此,self 是“SearchHotelViewController”。


如果一个类 (..)View 是 UIViewController 的子类,那么命名一个类 (..)View 会让人感到困惑。
H
Hardik Thakkar

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

 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

如果有两个自定义单元格,我需要知道在此索引路径上返回哪种类型的单元格。获取类型的最佳方法是什么?
C
Celil Bozkurt

对于斯威夫特 4

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

H
Hardik Mamtora

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];

D
David Kanarek

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

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

嗨,大卫,我重写了这个问题,你能再看一遍吗
O
Oscar Falmer

斯威夫特 3

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

K
Krunal

迅速

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

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅