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

问题描述:

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

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

- (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]; 

} 

有没有人有这是为什么每次都创建一个新的细胞,而不是给我的任何想法对被挖掘的单元格的引用?

我真的很感激任何帮助。

+0

它可能与这行代码有关:cell = [[AccountsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];你在哪里创建一个新的单元格,如果tableview没有退出。为什么你的表格视图不能让一个单元出队? – Alex 2015-01-09 18:28:38

+0

确保你注册你的表视图为你的重用标识符,它可能每次调用cellForRowAtIndexPath总是创建一个新的单元格,因为该标识符没有注册到tableview – 2015-01-09 18:36:34

+0

要以编程方式注册它,请使用'[tableView registerClass: [AccountsTableViewCell class] forCellReuseIdentifier:CellIdentifier];' – 2015-01-09 18:38:41

你基本上通过重新重新运行tableView:cellForRowAtIndexPath方法与tableViewindexPath作为参数创建一个新的AccountsTableViewCell

而是写的:

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

尝试

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

indexPath得到tableView的当前单元格。

+0

对此我无法表示感谢,这已经让我发疯了,你可能只是救了我的工作。 – SeanT 2015-01-09 20:56:00

+0

@SeanT哈哈没问题。很高兴你不会失去你的工作。 ;) – 2015-01-09 20:56:45