2011-12-30 2 views
0

, поэтому у меня есть много ячеек в моем UITableView и при прикосновении к ячейке, дополнительные ячейки, которые только что вне поля зрения, также имеют тики. Я считаю, что это происходит потому, что клетки повторного использования или что-то из-за dequeueReusableCellWithIdentifier:Проблема с множественным UITableViewCellAccessoryCheckmark, применяемым на одном касании UITableCell

Heres мой код:

- (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"]; 

Кто-нибудь знает как это исправить? Спасибо, ребята!

Dex

+0

Почтовый код для вашего метода cellForRowAtIndexPath datasource – 0x8badf00d

+0

@ 0x8badf00d добавлен! – DexCurl

+1

Где находится ваш cell.accessoryType = UITableViewCellAccessoryNone; в методе cellForRowAtIndexPath? и вносить изменения в ваш метод делегата в соответствии с ответом @samfisher. – 0x8badf00d

ответ

-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; 
Смежные вопросы