Использование вспомогательного флажка в табличном представлении - PullRequest
2 голосов
/ 08 августа 2011

У меня есть массив из 8 элементов, помещенных в табличное представление. Я использую следующий код для установки флажка при нажатии строки

[[tableView cellForRowAtIndexPath: indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

. Но моя проблема - 1] всякий раз, когда я касаюсь своей первой строки, также появляется флажок пятой строки.И 2], когда я прокручиваю табличное представление после проверки строки и возвращаюсь назад, отмеченная отметка исчезает.Как избавиться от этих проблем.заранее спасибо

вот код

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

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    // find the cell being touched and update its checked/unchecked image
   CellView *targetCustomCell = (CellView *)[tableView cellForRowAtIndexPath:indexPath];
    [targetCustomCell checkAction:nil];

    if ([arrData count]>0) 
    {
        if ([arrData containsObject:[arrName objectAtIndex:indexPath.row]]) {
            [arrData removeObject:[arrName objectAtIndex:indexPath.row]];
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];   
            NSLog(@"data removed");
            NSLog(@"arrData%@",arrData);
        }
        else {
            [arrData addObject:[arrName objectAtIndex:indexPath.row]];
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];   
            NSLog(@"data added");
            NSLog(@"tableArray%@",arrData);
        }

    }
    else {
        [arrData addObject:[arrName objectAtIndex:indexPath.row]];
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];   
        NSLog(@"data added");
        NSLog(@"arrData%@",arrData);
    }
   } 

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

        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
        if (cell == nil)
        {
        cell = (CellView *)[[[CellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
        }
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:13];  
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.text = [arrName objectAtIndex:indexPath.row];
        return cell;
    }

1 Ответ

1 голос
/ 08 августа 2011

для вашей 1) проблемы у вас есть код

2) проблема:

вам нужно запомнить последнюю выбранную строку, для которой вам нужно сохранить последний выбранный номер индекса:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    selectedIndex = indexPath.row;
    [tableView reloadData]; 
}

и в вашем

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if (indexPath.row == selectedIndex) 
  {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
     cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

надеюсь, это поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...