2014-10-20 6 views
0

У меня есть UITableView с несколькими разрешениями, но по какой-то причине, когда я просматриваю UITableView, выбранные мной параметры с UITableViewCellAccessoryCheckmark повторяются при прокрутке.UITableViewCell accessoriesType UITableViewCellAccessoryCheckmark повторяется

Это код, который я использую

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.backgroundColor = [UIColor clearColor]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = [sortedMachineNames objectAtIndex:indexPath.row]; 

    return cell; 
} 

#pragma mark - Table view delegate 

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath: 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [selectedMachinesMArray addObject:cell.textLabel.text]; 
} 

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

    cell.accessoryType = UITableViewCellAccessoryNone; 
    NSUInteger index = [selectedMachinesMArray indexOfObject:cell.textLabel.text]; 
    if (index!=NSNotFound) { 
     [selectedMachinesMArray removeObjectAtIndex:index]; 
    } 
} 

помощь будет оценена.

+0

«Однако по какой-то причине ...» Причина в том, повторное использование клеток. Сделайте поиск по «отметке галочки», и вы найдете много ответов. – rdelmar

ответ

2

В cellforIndexAtPath сделать проверку текущего состояния клетки и изменить значение типа accesory, как это:

if(marked) 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
else 
    cell.accessoryType = UITableViewCellAccessoryNone; 
+0

Какова наилучшая практика для маркировки? должен ли я создать совершенно отдельный массив с T/F для каждого UITableViewCell в UITableView? – HurkNburkS

+1

OKay Я только что сделал это, создал массив той же длины, что и UITableView, чтобы каждое значение в массиве было false, а затем, когда есть выбор или отмена, я делаю это значение в массиве истинным или ложным. – HurkNburkS

+1

@HurkNburkS Это правильно, у меня есть проблемы в начале, для графического аспекта вы можете использовать «WillDisplayCell: forRowAtIndexPath» P.D, если вы найдете мой ответ полезным, не могли бы вы upvoteme? – Darklex

Смежные вопросы