4
votes

I need to select multiple row in tableview for iphone 4.3.should change background color of cell if selected with out accessory type.after navigating to multiple view,if I get back to table view need to show the selected cells.

I am able to do multiple selection,this is the Logic which i have implemented

in cell for Row at IndexPath:

    cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
            cellButton.tag = indexPath.row;
            cellButton.frame = CGRectMake(POSITION_LABEL_X, POSITION_LABEL_Y, 320, 60);
            cellButton.backgroundColor = [UIColor clearColor];
            [cellButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:cellButton];

if(self.selectedRows && [self.selectedRows containsObject:indexPath])
    {
        UIImage *rowBackground = [UIImage imageNamed:@"cellBG.png"];
    ((UIImageView *)cell.backgroundView).image = rowBackground;
     cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)] autorelease];
  cell.selectedBackgroundView.backgroundColor = [[[UIColor alloc] initWithRed:0.725 green:0.894 blue:1 alpha:1] autorelease]; 
    }

    else {

        cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    }

Gave Button action:

-(void) buttonPressed:(UIButton *)sender
{
    UITableViewCell* selectedCell =  (UITableViewCell*)sender.superview.superview;
    if([sender isSelected])
    {
        [selectedCell setSelected:NO];
        //sender.backgroundColor = [UIColor clearColor];
        [selectedCell setBackgroundColor:[UIColor clearColor]];
        [self.selectedRows removeObject:[NSNumber numberWithInt:sender.tag]];
                        [sender setSelected:NO];
    } 
    else 
    {
        [selectedCell setSelected:YES];
        sender.backgroundColor = [[UIColor alloc] initWithRed:0.725 green:0.894 blue:1 alpha:1];
        //[selectedCell setBackgroundColor:[[UIColor alloc] initWithRed:0.725 green:0.894 blue:1 alpha:1]];
        [self.selectedRows addObject:[NSNumber numberWithInt:sender.tag]];
        [sender setSelected:YES];
    }
}

I am saving the selected cell values in database and able to retrive those values again coming back to tableView but not getting how to use those values to make cell as selected.

-(void) highlightSelectedValue
{
    [self.selectedRows removeAllObjects];
    NSLog(@"response %@",self.taskResponse.response);
    NSArray *selected = [self.taskResponse.response componentsSeparatedByString:@","];
    for (NSString *s in selected) {
        [self.selectedRows addObject:[NSNumber numberWithInt:([s intValue] - 1)]];
        UIButton *selectedButton = (UIButton*)cellButton;
            [selectedButton setSelected:NO];
        [selectedButton setTag:[s integerValue] -1 ]; 
        [self buttonClicked:selctedButton];
       [self.selectionTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:([s integerValue] - 1) inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }
}

Thanks.

1

1 Answers

1
votes

Try this code.It may help you to resolve your issue

-(void) highlightSelectedValue
{
    [self.selectedRows removeAllObjects];
    NSLog(@"response %@",self.taskResponse.response);
    NSArray *selected = [self.taskResponse.response componentsSeparatedByString:@","];
    for (NSString *s in selected) {
        [self.selectedRows addObject:[NSNumber numberWithInt:([s intValue] - 1)]];

        UITableViewCell *selectedCell = [self.selectionTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:([s integerValue] - 1) inSection:0]];
        [selectedCell setBackgroundColor:[[[UIColor alloc] initWithRed:0.725 green:0.894 blue:1 alpha:1] autorelease]];
        [self.selectionTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:([s integerValue] - 1) inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }
}