2011-11-29 193 views
1

我有一個UITableView和我編程地向單元格添加兩個按鈕。 1按鈕添加到單元格文本(向上計數)另一個減去1(倒計數)。但是,可以說我添加4,單元格的文本將爲4,但是當我將該單元格向上滾動並從視圖中移出時,當它回到視圖中時,單元格文本將返回到1,這是它開始的位置。同樣的情況發生,如果我添加(它也是相同的,如果我減去)單元格文本和切換頁面,然後返回到表視圖。這裏是cellForRow當滾動表格視圖單元格的視圖,單元格文本更改

- (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]; 
     newBtn = [[UIButton alloc]init]; 
     newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [newBtn setFrame:CGRectMake(260,20,55,35)]; 
     [newBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [newBtn setTitle:@"-" forState:UIControlStateNormal]; 
     [newBtn setEnabled:YES]; 
     [cell addSubview:newBtn]; 

     subBtn = [[UIButton alloc]init]; 
     subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [subBtn setFrame:CGRectMake(200,20,55,35)]; 
     [subBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [subBtn setTitle:@"+" forState:UIControlStateNormal]; 
     [subBtn setEnabled:YES]; 
     [cell addSubview:subBtn]; 
    } 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];  
    cell.textLabel.text = [cells objectAtIndex:indexPath.row]; 

return cell; 
} 

任何和所有幫助表示讚賞!謝謝:d

Here is the screen Shot of the cell

方法按鈕

- (IBAction)addLabelText:(id)sender{  
    cell = (UITableViewCell*)[sender superview];  
    cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1]; 
} 

- (IBAction)subtractLabelText:(id)sender 
{ 
    cell = (UITableViewCell*)[sender superview];   
    if ([[cell.textLabel text] intValue] == 0){ 
     cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +0]; 
    } 
    else{ 
     cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] -1]; 
     //[myTableView reloadData]; 

    } 
} 
+0

這是因爲細胞被重用,你需要(重新)中的cellForRowAtIndexPath設置的東西(標籤,按鈕),以確保,當細胞被重用,對細胞的所有組件都是正確的。例如,如果單元格上有一個按鈕,並且其文本每行都不相同,則該文本需要在cellForRowAtIndexPath中重置;如果所有行的文本都是相同的,顯然沒有什麼可擔心的。 –

+0

非常感謝幫助彼得!你如何建議我重置'cellForRowAtIndexPath'中的單元格文本?非常感謝! :D – iProRage

回答

2

你需要這樣的事情,你在哪裏儲存在細胞外的值。這是因爲細胞被重複使用,並且不能長期保存。

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

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 

     subBtn = [[UIButton alloc]init]; 
     subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [subBtn setFrame:CGRectMake(200,20,55,35)]; 
     [subBtn addTarget:self action:@selector(addLabelText:indexPath.row) forControlEvents:UIControlEventTouchUpInside]; 
     [subBtn setTitle:@"+" forState:UIControlStateNormal]; 
     [subBtn setEnabled:YES]; 
     [cell addSubview:subBtn]; 
    } 

    // we're loading the value from the array each time the cell is displayed. 
    cell.textLabel.text = [cellLabelValues objectAtIndex:indexPath.row]; 

return cell; 
} 

- (IBAction)addLabelText:(int)currentRow{  
    NSString *newValue = [NSString stringWithFormat:@"%d",[[[cellLabelValues objectAtIndex:currentRow] intValue] +1]; 
    // we update the value in the array since this is the source of the data for the cell 
    [cellLabelValues replaceObjectAtIndex:currentRow withObject:newValue]; 
    // now reload to get the new value 
    [myTableView reloadData]; 
} 
+0

也,謝謝你的幫助! :D當你和彼得說我必須設定變化的值時,你們是什麼意思?再次感謝! – iProRage

+1

我認爲你的值存儲在* cells *數組中,對嗎?在更改* cells *數組中的值後,調用ReloadData更新表格的單元格。我認爲這是核心答案。 –

+0

是的,這是正確的!哦! Geeze我想我明白了!感謝芽生病現在嘗試它,讓你知道它是怎麼回事! – iProRage

相關問題