Zoltan Varadi
Zoltan Varadi

Reputation: 2488

iOS uitableview cell only gets selected for a moment

In my code i want to select a cell in a UITableView. I perform the selection here, but every time it should be selected it gets selected only for a moment and then it loses its selection.

What did I do wrong?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableIdentifier"];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"SimpleTableIdentifier"];
    }

    cell.textLabel.text = [[self tableViewArray] objectAtIndex:indexPath.row];

    if(indexPath.row == self.selectedTopicIndex)
    {
        NSLog(@"DIE DIE DIE DIE DIE!!!");
        [cell setSelected:YES];
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
        [cell setSelected:YES];
    }

    return cell;
}

Upvotes: 0

Views: 109

Answers (1)

Alladinian
Alladinian

Reputation: 35636

Just move your selection logic inside -tableView:willDisplayCell:forRowAtIndexPath: method. Something like this:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == self.selectedTopicIndex)
    {
        NSLog(@"DIE DIE DIE DIE DIE!!!");
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
        [cell setSelected:YES];
    }
}

Upvotes: 1

Related Questions