2012-01-17 80 views
0

我有一個UITableViewCell。我可以從單元的textLabel加減1,我也可以刪除單元格。這是我的問題,可以說我給textLabel的值加5。而這個單元格位於0 indexPath(表格中的第一個單元格)。當我刪除這個單元格並且現在表格中不再有任何單元格時,我添加一個新單元格,並且這個新單元格自動獲得與剛被刪除的單元格相同的值。所以這個新單元格的值將爲5,我希望單元格的值爲1,就像每個單元格添加時一樣。只有當一個單元格被刪除並且一個新的單元格被添加到完全相同的indexPath時,纔會發生這種情況。所以我的問題是:我是否必須刪除這個單元格「內存」或「數據」才能解決這個問題?感謝一幫幫忙!刪除UITableViewCell和該單元格的所有數據

的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     addBtn = [[UIButton alloc]init]; 
     addBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [addBtn setFrame:CGRectMake(220,10,25,55)]; 
     [addBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [addBtn setTitle:@"+" forState:UIControlStateNormal]; 
     [addBtn setEnabled:YES]; 
     [cell addSubview:addBtn]; 

     subBtn = [[UIButton alloc]init]; 
     subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [subBtn setFrame:CGRectMake(260,10,25,55)]; 
     [subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [subBtn setTitle:@"-" forState:UIControlStateNormal]; 
     [subBtn setEnabled:YES]; 
     [cell addSubview:subBtn]; 

     //cell.textLabel.text = @"1"; 


    } 
    //cellText.hidden=!self.editing; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [number objectAtIndex:indexPath.row]; 


return cell; 
} 
+0

你可以發佈'cellForRowAtIndexPath:'方法的代碼嗎?這聽起來像是你在'dequeueReusableCellWithIdentifier:' – jonkroll 2012-01-17 05:22:09

+0

在你的UITableView上發佈了一些代碼,其中textLabel的值來自哪裏,你沒有正確配置textLabel的值?數組?或者其他什麼,也可能是因爲這個單元正在被重用。發佈你的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)索引路徑如果可能 – 2012-01-17 05:23:38

+0

@jonkroll肯定我會發布它吧! – iProRage 2012-01-18 00:07:46

回答

2

當細胞被刪除或熄滅屏幕,表視圖節省了他們,後來他們重用。所以你需要重置textLabel的值tableView:cellForRowAtIndexPath:。該UITableViewCell類引用這樣說:

表視圖的中的tableView委託:的cellForRowAtIndexPath:重用小區時應該始終重置所有內容。

+0

好的,我應該怎麼做?我應該設置'cell.textLabel.text = @「1」;還是有另一種方法來做到這一點?我也應該把它放在'if(cell == nil)'語句中嗎?非常感謝您的幫助!! – iProRage 2012-01-18 00:06:44

+0

請幫忙!我已經查找了答案,並且查看了文檔,謝謝! – iProRage 2012-01-18 02:44:57

+1

您需要將'cell.textLabel.text'初始化爲適合'indexPath'的值。通常這意味着你需要使用'indexPath.row'(或者'indexPath.section')來查找數組或數據庫表中的適當值。 – 2012-01-18 03:26:34

相關問題