gpt4 book ai didi

iphone - 如何在数组中包含 UITableViewCell 引用对象?

转载 作者:行者123 更新时间:2023-11-28 18:22:20 25 4
gpt4 key购买 nike

我有一个包含多个实例变量(nameaddressnumber 等)的对象数组。我还有一个 UITableViewController,其 TableView 中填充了每个对象的 name 变量。

我还创建了一个 DetailViewController,当使用 tableView:didSelectRowAtIndexPath: 选择对象的指定单元格时,它应该显示这些对象所持有的其余信息。问题是,这些单元格只引用了单元格对象的 name 变量。

我应该如何解决这个问题?我是否需要对单元格进行子类化,以便为每个单元格提供对整个对象的引用?或者有更简单的方法吗?

这是我当前的 tableView:cellForRowAtIndexPath: 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = [[listings objectAtIndex:indexPath.row] objectForKey:@"listingName"];
return cell;
}

更新:我突然想到我可以从 indexPath 中获取行号并从数组中获取指定的对象。这会更可行吗?

最佳答案

tableView:didSelectRowAtIndexPath: 中,您可以只使用 [listings objectAtIndex:indexPath.row] 获取所选对象,并将其传递给详细 View Controller 。

(您永远不应该尝试将表格 View 单元格用作数据源。)

关于iphone - 如何在数组中包含 UITableViewCell 引用对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18025535/

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