2016-01-23 99 views
0

在我的TableView didSelectRowAtIndexPath正在更新只在每個單元格的表的最後一個可見單元格是選擇。在UiTableView didSelectRowAtIndexPath更新錯誤單元格

下面是示例代碼:從細胞

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([tableView isEqual:subcategoryTV]) 
    { 
     selCell=(MDTableViewCell *)[subcategoryTV cellForRowAtIndexPath:indexPath ]; 

     if ([checkButton.titleLabel.text isEqualToString:@"✓"]) 
     { 
      checkButton.backgroundColor=[UIColor clearColor] ; 
      [checkButton setTitle:resetTitle forState:UIControlStateNormal]; 
      [checkButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 
      checkButton.layer.borderColor = [UIColor lightGrayColor].CGColor; 
     } 
     else 
     { 
      resetTitle=checkButton.titleLabel.text; 
      NSLog(@"%@",resetTitle); 

      checkButton.backgroundColor=[UIColor colorWithRed:1 green:0.839 blue:0.314 alpha:1] ; 
      [checkButton setTitle:@"✓" forState:UIControlStateNormal]; 
      [checkButton.titleLabel setFont:[UIFont fontWithName:@"Futura" size:25]]; 
      [checkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
      checkButton.layer.borderColor = [UIColor clearColor].CGColor; 
     } 

     if (costLabel.hidden==YES) { 
      costLabel.hidden=NO; 
     } 
     else 
     { 
      costLabel.hidden=YES; 
     } 
    } 
} 
+0

什麼是checkButton和costLabel?他們是否在一個單元格中的按鈕和標籤?您正在初始化'selCell',但未在所提及的代碼中使用或修改。 – UditS

+0

我懷疑'checkButton'和'costLabel'是那些被賦值給'cellForItemAtIndexPath:'單元格上的按鈕和標籤的屬性。這就是爲什麼最後一個單元格僅被更改的原因,因爲checkButton和costLabel被分配到最後一個單元格按鈕和標籤。 –

+0

@FahriAzimov checkButton和costLabel是我的自定義按鈕和單元格的標籤。 – PalviRane

回答

1

你不應該查詢數據,無論如何,你應該檢查你的模型來決定做什麼。當用戶更改任何行或按鈕時,更新模型,然後使用它更新視圖。這是正確的做法。

在這裏,你的具體問題,你的壞方法是,你得到的單元格在選定的索引路徑,然後不使用它。你應該從單元格中獲得按鈕和標籤。

相關問題