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

Sea*_*anT 0 iphone objective-c uitableview ios

我有一个表,当我点击一个单元格时,我希望能够访问该单元格,这样我就可以更改该单元格的某些属性,但由于某种原因,它每次都会创建一个新单元格.我可以说这是因为- (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;

}
Run Code Online (Sandbox Code Playgroud)

并选择一个单元格:

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

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

}
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么每次创建一个新单元格而不是给我一个被挖掘的单元格的引用?

我非常感谢任何帮助.

Lyn*_*ott 5

您实际上是AccountsTableViewCell通过tableView:cellForRowAtIndexPath使用tableViewand indexPath和参数重新运行方法来创建新的.

而不是写:

AccountsTableViewCell *cell = (AccountsTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

尝试

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

得到tableView当前的单元格indexPath.