user941059
user941059

Reputation: 157

Single row selection - UITableView

Good Afternoon,

I'm trying to deploy my application in the selection of a single line in UITableView, but when I select another row, the previous line is not clear. I'm using images to represent the selection of the lines. I've tried to make each user's selection table rows stay with the image of deselection, but got no success.

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

    static NSString *EditCellIdentifier = @"EditCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:EditCellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:EditCellIdentifier] autorelease];

        UILabel *label = [[UILabel alloc] initWithFrame:kLabelRect];
        label.tag = kCellLabelTag;
        [cell.contentView addSubview:label];
        [label release];

        UIImageView *imageView = [[UIImageView alloc] initWithImage:unselectedImage];
        imageView.frame = CGRectMake(5.0, 10.0, 23.0, 23.0);
        [cell.contentView addSubview:imageView];
        imageView.hidden = !inPseudoEditMode;
        imageView.tag = kCellImageViewTag;
        [imageView release];
    }

    [UIView beginAnimations:@"cell shift" context:nil];

    UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:kCellImageViewTag];
    NSNumber *selected = [selectedArray objectAtIndex:[indexPath row]];
    imageView.image = ([selected boolValue]) ? selectedImage : unselectedImage;
    imageView.hidden = !inPseudoEditMode;
    [UIView commitAnimations];

    UILabel *label = (UILabel *)[cell.contentView viewWithTag:kCellLabelTag];
    label.text =[listOfItems objectAtIndex:[indexPath row]];
    label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
    label.opaque = NO;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tv deselectRowAtIndexPath:indexPath animated:YES];

    if (inPseudoEditMode) {

        BOOL selected = [[selectedArray objectAtIndex:[indexPath row]] boolValue];
        [selectedArray replaceObjectAtIndex:[indexPath row] withObject:[NSNumber numberWithBool:!selected]];
        [tv reloadData];
    }
}

Upvotes: 4

Views: 3714

Answers (2)

Shoaib
Shoaib

Reputation: 21

Shortest way which i use


for (int i = 0; i < [selectedArray count]; i++) {
    [selectedArray replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:i==indexpath.row?YES:NO]];
}

Upvotes: 1

Pierre-David Belanger
Pierre-David Belanger

Reputation: 1054

I guess you want to allow only one row selected at a time.

Here is what I suggest in tableView: didSelectRowAtIndexPath::

Mark all row as not selected:

for (int i = 0; i < [selectedArray count]; i++) {
    [selectedArray replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
}

Then, mark the current row as selected:

[selectedArray replaceObjectAtIndex:[indexPath row] withObject:[NSNumber numberWithBool:YES]];

OR, do it all in the same for loop:

for (int i = 0; i < [selectedArray count]; i++) {
    NSNumber rowSelected = [NSNumber numberWithBool:(indexPath.row == i)];
    [selectedArray replaceObjectAtIndex:i withObject:rowSelected];
}

Hope this is what you want!

Upvotes: 4

Related Questions