gpt4 book ai didi

ios - 点击 UITableView 单元格正在创建一个新单元格

转载 作者:行者123 更新时间:2023-11-28 18:31:42 26 4
gpt4 key购买 nike

我有一个表格,当我单击一个单元格时,我想访问该单元格以便我可以更改该单元格的某些属性,但由于某种原因它每次都会创建一个新单元格。我可以这么说,因为每次我点击一个单元格时,都会调用 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

这是我创建单元格的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"AccountsCell";

NSLog(@"AccountsTableViewController : cellForRow");

AccountsTableViewCell *cell = (AccountsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (nil == cell)
{

cell = [[AccountsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

[cell setCurrentAccount:[self.accounts objectAtIndex:indexPath.row]];

cell.delegate = self;

return cell;

}

选择一个单元格:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

AccountsTableViewCell *cell = (AccountsTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];

}

有谁知道为什么每次都创建一个新的单元格而不是给我一个被点击的单元格的引用?

非常感谢任何帮助。

最佳答案

您实际上是在创建一个新的 AccountsTableViewCell通过重新运行 tableView:cellForRowAtIndexPath方法与 tableViewindexPath作为参数。

而不是写:

AccountsTableViewCell *cell = (AccountsTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];

尝试

AccountsTableViewCell *cell = (AccountsTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

获取tableView当前单元格位于 indexPath .

关于ios - 点击 UITableView 单元格正在创建一个新单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27866755/

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