簡體   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