繁体   English   中英

点击UITableView单元格将创建一个新单元格

[英]Tapping a UITableView cell is creating a new cell

我有一张桌子,当我单击一个单元格时,我想访问该单元格,以便可以更改该单元格的某些属性,但是由于某种原因,它每次都会创建一个新的单元格。 我之所以这么说是因为- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier每次我点击一个单元格时都会调用- (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];

}

有谁知道为什么每次都要创建一个新单元,而不是给我一个被窃听的单元的参考?

我非常感谢您的帮助。

您实际上是通过重新运行tableView:cellForRowAtIndexPath方法(使用tableViewindexPath作为参数)来创建一个新的AccountsTableViewCell

而不是写:

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

尝试

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

indexPath获取tableView的当前单元格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM