gpt4 book ai didi

ios - 返回 NULL 的 tableView 单元格

转载 作者:行者123 更新时间:2023-11-29 10:55:34 24 4
gpt4 key购买 nike

我正在尝试从 tableView 单元格中读取文本,在 prepareForSegue 方法中,我会获取文本并将其设置为 NSUserDefaults。但是,当我 NSLog 文本时,它返回 NULL。这是我试过的。

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender
{
NSString *blah = sender.textLabel.text;
NSLog(@"%@",blah); // It returns NULL no matter what cell is clicked
}

最佳答案

您不应该使用单元格中的标签,因为它是 View 的一部分。虽然单元格是正确的,但它的视觉效果可能会在调用 prepareForSegue: 时被处理掉。

执行此操作的 MVC 方法是查询底层数据源以获取单元格行中的数据,并使用该数据而不是标签。

这就是我的意思:在 cellForRowAtIndexPath: 实现的某处,您有如下代码:

NSString *labelData = [myDataSource objectAtIndex:indexPath.row];
cell.textLabel.text = labelData;

您应该在 prepareForSegue: 方法中复制该代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender
{
NSUInteger row = [self.tableView indexPathForCell:sender].row
NSString *labelData = [myDataSource objectAtIndex:row];
NSLog(@"%@", labelData); // This should work
}

关于ios - 返回 NULL 的 tableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18563987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com