2013-10-08 22 views
0

我需要允許用戶爲某些項目選擇多個選項。爲此我使用以下內容。它需要兩個方法來取消UITableView中的accessoryType

唯一的問題是,我有選擇行兩次刪除accessoryView。我可以選擇第一個選擇的行。然而,選擇行後,如果觸摸它來取消選擇,首先只會突出顯示顏色,然後我必須再次單擊以取消選擇該行。

任何想法爲什麼會發生這種情況?我想在首次觸摸時取消選擇該行。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
int i=indexPath.row; 
if(self.multi == 1){ 

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSLog(@"accessoryType:%d",selectedCell.accessoryType); 
    if (selectedCell.accessoryType == UITableViewCellAccessoryNone) { 
     selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     NSString *cellText = selectedCell.textLabel.text; 
     [self.selectedValues replaceObjectAtIndex:i withObject:cellText]; 
    }else{ 
     selectedCell.accessoryType = UITableViewCellAccessoryNone;     
     [self.selectedValues replaceObjectAtIndex:i withObject:@""];   
    }   
} 
else{ 

    NSLog(@"not for multi select",nil); 
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *cellText = selectedCell.textLabel.text; 
    [self.delegate addItemViewController:self setPeculiarity:cellText setIndex:self.index]; 

    [self dismissModalViewControllerAnimated:YES]; 
} 
} 

編輯:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
    cell.textLabel.text = [pecs objectAtIndex:indexPath.row]; 

if(self.selectedValues.count > 0){ 
    for(int k = 0 ; k < [selectedValues count];k++){ 

     if ([[self.selectedValues objectAtIndex:k] isEqualToString:[pecs objectAtIndex:indexPath.row]]) { 

      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
       NSLog(@"pecs:%@",[pecs objectAtIndex:indexPath.row]); 
     } 
    } 
} 
// for background color 
UIView *bgColorView = [[UIView alloc] init]; 
bgColorView.backgroundColor = [UIColor colorWithRed:0.2823 green:0.4509 blue:0.8588 alpha:1.0]; 
[cell setSelectedBackgroundView:bgColorView]; 

cell.textLabel.highlightedTextColor = [UIColor whiteColor]; 
return cell; 
} 

回答

0

因爲沒有人回答了這個問題,我必須做我自己。

我終於通過使用[tableView reloadData]解決了這種情況。

我可以爲所有ios用戶添加一個通知,並且在對數據表進行任何更改後重新加載表。

1

如果您將使用[tableView reloadData]整個tableView將被重新加載。

它不會佔用太多內存&在模擬器上的執行時間,但在真正的iPhone/iPad/iPod上它會佔用更多的資源。

更換[tableView reloadData][tableView reloadRowsAtIndexPaths: [NSArray arrayWithObjects: [NSIndexPath indexPathForRow: indexPath.row inSection: indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];

您可以

UITableViewRowAnimationFade, 
UITableViewRowAnimationRight, 
UITableViewRowAnimationLeft, 
UITableViewRowAnimationTop, 
UITableViewRowAnimationBottom, 
UITableViewRowAnimationNone, 
UITableViewRowAnimationMiddle, 
UITableViewRowAnimationAutomatic = 100 
+0

這是一個好的技巧取代UITableViewRowAnimationNone。但這種解決方案正在發生一件奇怪的事情。每當我選擇任何一行時,上面的分隔線對於當前單元格變得不可見。任何猜測爲什麼? – astuter

+0

添加您的'cellForRowAtIndexPath' .. –

+0

仍然具有相同的奇怪效果。 – astuter

相關問題