brianhevans
brianhevans

Reputation: 1193

Select specific string value in UITableView's cell

I have a table view with a list of items from the AppDelegate. The table view allows the user to select the item they want and there is a checkmark next to the item after the selection has been made. Next the user can go back to the previous screen and this selection updates a string value which is displayed to the user. This value is also stored in the AppDelegate. Everything works well but I would like this value to be automatically selected if the user chooses to GO BACK into the screen where the user can change their selection.

Here is the cellFroRowAtIndexPath function in my SetSizeViewController.m

I am having trouble getting the value stored in appDelegate.selectedSize to be selected. This value, for example, could be something like 'Meter'.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 self.checkedIndexPath = indexPath;

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

UITableViewCell *result = nil;

static NSString *CellIdentifier = @"CellIdentifier";

result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

TabAndSplitAppAppDelegate *appDelegate = (TabAndSplitAppAppDelegate *)[[UIApplication sharedApplication] delegate];
SetSize *setSize = (SetSize *)[appDelegate.setSizes objectAtIndex:indexPath.row];

appDelegate.selectedSize = setSize.name;

appDelegate.selectedSizeCheckedIndexValue = (NSInteger *)indexPath.row;

[tableView reloadData];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

TabAndSplitAppAppDelegate *appDelegate = (TabAndSplitAppAppDelegate *)[[UIApplication sharedApplication] delegate];

if(appDelegate.selectedSize.length > 0)
{
    if ((int)indexPath.row == (int)appDelegate.selectedSizeCheckedIndexValue) {

        [cell setSelected:YES animated:YES];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        [cell setSelected:NO];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
}

Upvotes: 0

Views: 971

Answers (1)

KDaker
KDaker

Reputation: 5909

you should avoid calling didSelectRowAtIndexPath: yourself. anyway, you should try calling [result setSelected:YES animated:YES].

also, how are you checking if the this cell is the one to be selected? correct me if im wrong but it seems to me that with this if(appDelegate.selectedSize) { ..., every cell is going to be true for the selection code.

EDIT:

remove [result setSelected:YES animated:YES] from cellForRowAtIndexPath and add the following:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == mySelectedRow) { //mySelectedRow is the index value you stored previously.

        cell.selected = YES; //or [cell setSelected:YES animated:YES] if you want it to be animated.
    }
}

EDIT 2:

first adjust your if function in willDisplayCell: just in case to this:

if ((int)indexPath.row == (int)appDelegate.selectedSizeCheckedIndexValue) {

    [cell setSelected:YES animated:YES]; //or animated:NO depends on what you need.
    [self performSelector:@selector(unselectCellAtIndexPath:) withObject:indexPath afterDelay:1.0]; //add a delay to the deselection. you can change the delay time to whatever suits you.
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}
else {

    [cell setSelected:NO];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

-(void)unselectCellAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

then inside your didSelectRowAtIndexPath:, after you select your cell, call [tableView reloadData]

Upvotes: 1

Related Questions