2015-03-31 64 views
-1

我有一個UITableView,它包含一個tableView,它可以從一個可變數組中填充'原型單元格',並且在選擇它們時顯示'附件複選標記'。我在tableview下面的視圖中有一個textfield,然後將其數據附加到填充tableview的數組。我的問題是,在我追加新的數據添加一個單元格到tableview後,我必須觸摸一個單元格兩次,以取消選擇任何我先前選擇的單元格。iOS:UITableView必須雙擊才能取消選擇

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"skillName" forIndexPath:indexPath]; 

    cell.textLabel.text = skillsOptions[indexPath.row]; 

    // Configure the cell... 

    return cell; 
} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    //tableViewCell.accessoryView.hidden = NO; 
    tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    //tableViewCell.accessoryView.hidden = YES; 
    tableViewCell.accessoryType = UITableViewCellAccessoryNone; 
    [skillsList removeObject:skillsOptions[indexPath.row]]; 
} 

-(void)grabSelectedSkills { 

    for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) { 
     NSString *skill = skillsOptions[indexPath.row]; 
     //NSString *skill = [NSString stringWithFormat:@"%li",(long)indexPath.row]; 
     [skillsList addObject:skill]; 
    } 
    NSLog(@"skillsList: %@",skillsList); 
} 
- (IBAction)continue:(id)sender { 
    [self grabSelectedSkills]; 
} 
- (IBAction)addOtherSkills:(id)sender { 
    if (self.otherSkill.text.length > 1) { 
    [skillsOptions addObject:self.otherSkill.text]; 


    self.otherSkill.text = @""; 
    [self.tableView endEditing:YES]; 
    [self.tableView reloadData]; 
    } 
+0

您需要在單元格外跟蹤您的選擇/取消選擇狀態 - 「技能選項」鍵入的字典可能是一個不錯的選擇。然後,您可以在'cellForRowAtIndexPath'和'didSelectRowAtIndexPath'中使用它來根據需要添加/刪除複選標記。您應該取消選擇'didSelectRowAtIndexPath'中的行並刪除'didDeselectRowAtIndexPath' – Paulw11 2015-03-31 05:45:44

回答

0

您需要跟蹤您的選擇/取消選擇狀態 - 由「技能選項」鍵入的字典可能是一個不錯的選擇。

然後,您可以在cellForRowAtIndexPath和didSelectRowAtIndexPath中根據需要添加/刪除複選標記。您應該取消選擇didSelectRowAtIndexPath中的行並刪除didDeselectRowAtIndexPath -

創建self.selectedCells作爲NSMutableDictionary屬性。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"skillName" forIndexPath:indexPath]; 

    // Configure the cell... 

    NSString *skillOption=skillsOptions[indexPath.row]; 

    cell.textLabel.text = skillOption; 

    if (self.selectedCells[skillOption]!= nil) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSString *skillOption=skillsOptions[indexPath.row]; 

    if (self.selectedCells[skillOption]!= nil) { 
     tableViewCell.accessoryType = UITableViewCellAccessoryNone; 
     [self.selectedCells removeObjectForKey:skillOption]; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     self.selectedCells[skillOption]=skillOption; 
    } 
    [tableview deselectRowAtIndexPath:indexPath]; 
} 
+0

謝謝@ Paulw11這工作完美。 – user3255585 2015-04-01 09:27:07

0

我認爲你應該只使用didSelectRowAtIndexPath方法:方法(刪除didDeselectRowAtIndexPath:方法),並檢查選擇的單元格,如果它不是,反之亦然原樣

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    BOOL cellSelected = NO; 
    // set cellSelected based on your condition 
    if(cellSelected) 
    { 
     // code to deselect the cell 
    } 
    else 
    { 
     // code to select the cell 
     UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    //tableViewCell.accessoryView.hidden = NO; 
    tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
} 
+0

cellSelected做了什麼?我不明白它是如何被調用的。我必須將它綁定到某個東西上嗎? – user3255585 2015-03-31 06:06:43

+0

cellSelected只是一個BOOL變量,用於保存是否選中某個單元格的狀態,每次觸摸單元格時選中/未選中didSelecteRowAtIndexPath:將被調用 – 2015-03-31 06:12:52

+0

您不需要綁定任何東西,只需刪除deselectRowAtIndexPath:和doSelectRowAtIndexPath內取消選擇的東西:基於cellSelected BOOL狀態 – 2015-03-31 06:19:52

相關問題