2012-04-20 56 views
4

我需要在iphone 4.3的tableview中选择多行。如果选择了出附件类型,应该更改单元格的背景颜色。导航到多视图后,如果我回到表视图需要显示选定的单元格。Tableview for iPhone 4.3中的多个单元格选择

我能够做多项选择,这是我在IndexPath在细胞中实现

row的逻辑:

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]; 
    } 

给了按钮动作:

-(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]; 
    } 
} 

我我将选定的单元格值保存在数据库中,并且能够将这些值再次返回到tableView,但没有得到如何使用这些值来使单元格处于选定状态。

-(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]; 
    } 
} 

谢谢。

回答

1

试试这个code.It可以帮助你解决你的问题

-(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]; 
    } 
} 
+0

非常感谢它的工作。 – Srinivas 2012-04-20 07:39:35

+0

但如果我想取消选择cell.it没有发生。 – Srinivas 2012-04-20 09:18:08

+0

采取一些布尔值,并将其放在必要的地方 – siva 2012-04-20 09:20:29

相关问题