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

Discussion in 'iOS Programming' started by ataylor2009, Apr 8, 2010.

  1. ataylor2009 macrumors member

    Joined:
    Jan 27, 2009
    #1
    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:

    Code:
    - (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; }
    When a run this code in the debugger, it shows that labelText is equal to currentLabel - but the checkmark accessory is not being applied to the 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.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You are using == to compare strings. Don't. Look in the documentation for the correct way to compare them (hint do a search for Equal in the NSString page).
     
  3. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #3
    Doh! (Head-slapping sound, followed by crickets chirping in the background...)

    "Thanks," he said sheepishly.
     

Share This Page