iOS:UITableView单元格在滚动时更改选择状态

时间:2014-10-06 12:50:04

标签: ios objective-c uitableview

所以我不知道如何写一个解释我问题的简单标题。

我的屏幕上有一个UITableView。只要表格视图小于屏幕(所有单元格都适合屏幕而不滚动),一切都很好。但是,一旦用户需要滚动屏幕以找到位于表格底部的单元格,一些单元格就会随机更改其选择状态。 我像这样实现了UITableView:

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

    switch (indexPath.section)
    {
        case 1:
        {
            if (indexPath.row == 0)
            {
                simpleTableIdentifier = @"EventDateCell";
            }
            else
            {
                simpleTableIdentifier = @"EventDateSelectCell";
            }

            [...]

            return cell;
        }break;

        case 2:
        {
            simpleTableIdentifier = @"SelectPlayerCell";

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
            }

            Player *player = [self.playerController getPlayerAtPosition:indexPath.row];

            cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];

            return cell;
        }break;

        default:
        {
            simpleTableIdentifier = @"EventNameCell";

            [...]

            return cell;
        }break;
    }
}

xCode是否会根据屏幕上显示的单元格更改indexPath.row?

标记所选单元格的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1 && indexPath.row == 0)
    {
       [..]
    }
    else if (indexPath.section == 2)
    {
        // close date picker if it is still open
        if (self.selectingEventDate == TRUE)
        {
           [...]
        } 

        if(self.createdEvent != nil)
        {
            if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
            {
                [self tableView:tableView didDeselectRowAtIndexPath:indexPath];
            }
            else
            {
                [[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
                [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryCheckmark];

                [self.eventController addPlayerToEvent:self.createdEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
            }
        }
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1 && indexPath.row == 0)
    {
       [...]
    }
    else if (indexPath.section == 2)
    {
        if(self.createdEvent != nil)
        {
            [[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryNone];

            [self.eventController removePlayerFromEvent:self.createdEvent :[self.playerController getPlayerAtPosition:indexPath.row] :FALSE];
        }
    }
}

我发现:如果标记了第一个单元格,则会有12个单元格正确显示,并且会检查第13个单元格。

2 个答案:

答案 0 :(得分:5)

首先,您选择一个Array,它在您选择Cell时存储该值,并在单击第二个单元格时删除该值。

在ViewController.h文件中

@property(nonatomic,retain)NSMutableArray * arySelectionState;

在ViewController.m文件中

- (void)viewDidLoad
 {
    [super viewDidLoad];
    arySelectionState = [[NSMutableArray alloc]init];
 }

pragma mark - TableView委托

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

    custom_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellDetails" forIndexPath:indexPath];


    if ([arySelectionState containsObject:[NSNumber numberWithLong:indexPath.row]])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

    }



    [cell.labtest setText:[NSString stringWithFormat:@"Lable_%ld",(long)indexPath.row]];

    // Configure the cell...

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([arySelectionState containsObject:[NSNumber numberWithLong:indexPath.row]])
    {

        [arySelectionState removeObject:[NSNumber numberWithLong:indexPath.row]];

        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryNone];
    }
    else
    {

        [arySelectionState addObject:[NSNumber numberWithLong:indexPath.row]];

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


}

它的工作精细......

答案 1 :(得分:2)

仅在cellForRowAtIndexPath:中更改单元格的外观。如果您在其他位置进行更改,它们将应用于dequeueReusableCellWithIdentifier:返回给您的缓存单元格。

在这种情况下,给玩家一个"选择"属性,在需要时设置,并调用reloadData。在cellForRowAtIndexPath:中使用该属性来设置单元格选择状态。

相关问题