2011-10-18 51 views
0

我有一個UITableView,我可以添加和刪除單元格到這個表。我也有兩個按鈕。 1按鈕爲單元格的文本添加「1」,這是1,所以基本上按下+按鈕時會計數,按下按鈕時會減小。我的問題是與最後一個單元格。如果我添加5個單元格,它是有問題的第5個單元格。如果我添加了200個單元格,它的第200個單元格等。問題是當我按下 - 按鈕時,所有其他單元格在按下時保持轉動藍色,並且此按鈕停止轉動藍色。當單元格文本爲0時,它會保持白色。我希望它在按下時保持與所有其他單元格一樣變藍。這裏是我的代碼:tableView中的奇怪的bug

- (IBAction)subtractLabelText:(id)sender 
{ 
    cell = (UITableViewCell*)[sender superview];  

    if ([[cell.textLabel text] intValue] == 0){ 
     [newBtn setEnabled:NO]; 
    } 
    else{ 
     cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text  
     intValue] -1]; 
     [newBtn setEnabled:YES]; 
    } 
} 

此方法連接到子「 - 」按鈕。另外,當我按下按鈕時,文本是= 0,按鈕是在那裏,但是當我按下它,它選擇單元格和表格單元格變成藍色,就好像我選擇了!請幫忙!謝謝大家!

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]; 
    } 
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];  
    cell.textLabel.text = [cells objectAtIndex:indexPath.row]; 

    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]; 
    return cell; 
} 
+0

可以請您發佈'-cellForRowAtIndexPath'方法中的代碼 –

+0

是的,我很喜歡! – iProRage

回答

0

根據您的cellForRowAtIndexPath方法,您正在爲每個單元格創建並添加新按鈕,無論它們是正在重用還是「全新」。

您的代碼做這樣的事情:

  1. 檢查是否有出隊的單元/重複使用
  2. 如果是的話,抓住它的一個引用。如果不是,則創建一個新的。
  3. 添加按鈕XYZ到小區(你的錯誤就在這裏)
  4. 返回的實現代碼如下電池使用

試想一下,在步驟2中,如果您重用現有的小區,該小區已經有按鈕XYZ添加到它,所以你實際上增加了兩個額外的按鈕,當你不需要。

您應該將設置單元格按鈕的代碼移動到if (cell == nil)循環中,以便只在您初始化新的單元格對象時才創建按鈕並將其添加到內容視圖中。

此外請確保您查看下面的附加評論,這些評論與您的​​問題沒有直接關係,但可能與您的問題有關。

============================================== ====================

[編輯 - 回答之上,但它也與所以我離開了這裏]

這不會解決你的問題,但你應該將你的按鈕細胞的contentview而不是直接到小區,即:

[cell.contentView addSubView:buttonABC]; 

一旦這樣做了,你就需要調用上海華兩次讓你減去的單元格引用/加方法:

cell = (UITableViewCell*)[[sender superview] superview];  

從蘋果公司的文件上UITableViewCell

一個UITableViewCell對象的內容視圖是由單元格中顯示的內容默認的上海華 。如果要通過 簡單添加其他視圖來自定義單元格,則應將它們添加到內容 視圖中,以便在單元格轉換爲 進入和退出編輯模式時它們將被正確定位。

+0

@iProRage它實際上'addSubview'小寫V.還,我編輯了我的答案。 – Rog

+0

完美!!!!!:D非常感謝大家的幫助!!:D – iProRage

0

在猜測,我會說這有多麼的UITableViewCell s的被重用的事,在用戶界面不會「知道」有關這些單元格上的子視圖按鈕。

嘗試將-cellForRowAtIndexPath中的代碼註銷,使可重用單元出列(實際上,無論cell==nil與否,總是會創建新的UITableViewCell)。

編輯: 你引用的-subtractLabelTextnewBtn,但是這是伊娃並不一定是指發送消息的按鈕。改爲嘗試[sender setEnabled:NO]

+0

嗯,我試着註釋掉代碼和'if'語句,它沒有工作:(任何更多的想法?感謝您的幫助! – iProRage

+0

編輯答案... – cgull

+0

好吧,我試過這個,它傷心地沒有做任何事情...更多的想法?Thanks bud! – iProRage