How to manage a single-selection (exclusive) list?

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2010-04-09T03:39:12Z Indexed on 2010/04/09 3:43 UTC
Read the original article Hit count: 228

Filed under:
|

I've got a detail view that consists of a table view with a few rows of data. I can fill the rows just fine, and moving back and forth from the parent view to the subview works too. This detail view should allow the user to select a single value, placing a checkmark accessory in the cell and then returning to the parent view (where the selected value becomes the cell.textLabel.text property of the cell from which the detail view was called). This all works as of right now.

However, when the user taps the cell in the parent view to go back to the detail view (to change their selection), my checkmark has disappeared and I cannot for the life of me figure out how to make it stay.

Here's my cellForRowAtIndexPath: method:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }

    NSString *labelText = [[phones objectAtIndex:indexPath.row] valueForKey:@"phoneNumberLabel"];
    cell.textLabel.text = labelText;
    NSString *currentLabel = [ruleBuilder.tableView cellForRowAtIndexPath:selectedIndexPath].textLabel.text;
    if (labelText == currentLabel) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }

    NSString *detailText = [[phones objectAtIndex:indexPath.row] valueForKey:@"phoneNumber"];
    cell.detailTextLabel.text = detailText;
    return cell; }

I've checked out Apple's sample code for exclusive-list management in Table View Programming Guide, but the snippet seems incomplete, and I can't find the related code in Apple's sample code. This doesn't seem like it ought to be that hard to do.

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about uitableview