Razgriz
Razgriz

Reputation: 7343

iOS 7 - UITableView does not stay highlighted

Sorry if this is a simple problem, but my code does not seem to work properly. I want to keep the selected rows in a tableView highlighted after the user selects it.

I have 2 arrays where I take the price and item name from. I have a third array that keeps track of which row was selected so I know which ones have been selected already.

Here is my cellForRowAtIndexPath method where I assign the text label and the check mark.

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

    static NSString *ItemCellIdentifier = @"CellIdentifier";

    //SearchresultTableViewCell is my own class for the tableViewCell
    SearchResultTableViewCell *tableCell =(SearchResultTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ItemCellIdentifier];

    //set text labels here
    [tableCell.price setText:[NSString stringWithString:[priceList objectAtIndex:indexPath.row]]];
    [tableCell.itemName setText:[NSString stringWithString:[itemNameList objectAtIndex:indexPath.row]]];

    //set check mark and highlights.
    if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
        tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [tableCell setHighlighted:YES];
    }

    else{
        tableCell.accessoryType = UITableViewCellAccessoryNone;
        [tableCell setHighlighted:NO];
    }

    return tableCell;

}

And here is he didSelectRowAtIndexPath method. What I basically do here is I mark the object as checked by editing my checkList array.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"did select row at index path entered");
    if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
        [checkList replaceObjectAtIndex:indexPath.row withObject:@"0"];

    }
    else{
        [checkList replaceObjectAtIndex:indexPath.row withObject:@"1"];
    }
    [self.searchResults reloadData];
}

I have tried [tableCell setHighlighted:TRUE]; and [tableCell setHighlighted:1]; but it does not work.

Any ideas? Thank you.

Upvotes: 2

Views: 1849

Answers (3)

Yas Tabasam
Yas Tabasam

Reputation: 10615

I guess you need to call setSelected: not setHighlighted, please try replacing your [tableCell setHighlighted:YES]; with [tableCell setSelected:YES];

Moreover, I noticed that simply calling [tableCell setSelected:YES]; does not always select the row, so if setSelected still does not work, please try replacing [tableCell setHighlighted:YES]; with following:

[tableView selectRowAtIndexPath:indexPath 
                            animated:YES 
                      scrollPosition:UITableViewScrollPositionNone];

You can comment out [tableCell setHighlighted:NO]; since when you reload your table it will automatically get un selected.

This is how your code should look like:

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

    static NSString *ItemCellIdentifier = @"CellIdentifier";

    //SearchresultTableViewCell is my own class for the tableViewCell
    SearchResultTableViewCell *tableCell =(SearchResultTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ItemCellIdentifier];

    //set text labels here
    [tableCell.price setText:[NSString stringWithString:[priceList objectAtIndex:indexPath.row]]];
    [tableCell.itemName setText:[NSString stringWithString:[itemNameList objectAtIndex:indexPath.row]]];

    //set check mark and highlights.
    if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
        tableCell.accessoryType = UITableViewCellAccessoryCheckmark;

        // [tableCell setHighlighted:YES];

        [tableView selectRowAtIndexPath:indexPath 
                               animated:YES 
                         scrollPosition:UITableViewScrollPositionNone];
    }

    else{
        tableCell.accessoryType = UITableViewCellAccessoryNone;

        // [tableCell setHighlighted:NO];
    }

    return tableCell;

}

Upvotes: 3

bgfriend0
bgfriend0

Reputation: 1152

Thanks for answering my comment. As I mentioned, sending the reloadData message will cause the currently selected cells to be deselected. Therefore, you should cache the currently selected cells just before reloading the tableView and restore them afterwards. I'm thinking something like this:

NSArray *indexPaths = [self.searchResults indexPathsForSelectedRows];

[self.searchResults reloadData];

for (NSIndexPath *indexPath in indexPaths) {
    [self.searchResults selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

This should work as long as you aren't inserting/deleting/moving rows. If you are, you'll have to account for that.

Upvotes: 3

r.szeja
r.szeja

Reputation: 71

If you want to keep several cells selected at the same time, you should consider setting tableView.allowsMultipleSelection = YES;

Upvotes: 1

Related Questions