2011-12-30 42 views
0

因此,我的UITableView中有很多單元格,並且在觸摸單元格時,剛剛出現在視圖外的其他單元格也應用了刻度。我相信這是因爲細胞被再利用或因dequeueReusableCellWithIdentifier:在單個UITableCell觸摸上應用了多個UITableViewCellAccessoryCheckmark問題

繼承人我的代碼的東西:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

      if (cell.accessoryType == UITableViewCellAccessoryNone){ 
       [addPapersSelectedRowsArray addObject:[NSNumber numberWithInt:indexPath.row]]; 
       [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 

      } 
      else if (cell.accessoryType == UITableViewCellAccessoryCheckmark){ 
       cell.accessoryType = UITableViewCellAccessoryNone; 
       [addPapersSelectedRowsArray removeObject:[NSNumber numberWithInt:indexPath.row]]; 
      } 


    } 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 



    NSDictionary *currentPapersSecltion = [papersDataDictionary objectAtIndex:indexPath.row]; 
    [[cell textLabel] setText:[currentPapersSecltion objectForKey:@"Title"]]; 



    return cell; 

} 

也從一個在的.plist拉viewDidLoad數據:

//Load papers data from the local plist 
    papersDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"papers.plist"]] objectForKey:@"Rows"]; 

有誰知道如何解決這個問題? 謝謝你們!

敏捷

+0

您的cellForRowAtIndexPath數據源方法的郵政編碼 – 0x8badf00d 2011-12-30 15:38:15

+0

@ 0x8badf00d added! – DexCurl 2011-12-30 15:44:21

+1

Where is your cell.accessoryType = UITableViewCellAccessoryNone;在你的cellForRowAtIndexPath方法?並根據@samfisher答案對您的委託方法進行更改。 – 0x8badf00d 2011-12-30 16:00:56

回答

-1
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    // add this line 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    if (cell.accessoryType == UITableViewCellAccessoryNone){ 
     [addPapersSelectedRowsArray addObject:[NSNumber numberWithInt:indexPath.row]]; 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 

    } 
    else if (cell.accessoryType == UITableViewCellAccessoryCheckmark){ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [addPapersSelectedRowsArray removeObject:[NSNumber numberWithInt:indexPath.row]]; 
    } 
    //add this line as well 
    [tableView reloadData]; 
3

中,你需要設置根據小區是否已被選中的對號的cellForRowAtIndexPath。

if ([addPapersSelectedRowsArray containsObject:[NSNumber numberWithInt:[indexPath row]]]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
return cell;