适用于iPhone 4.3的Tableview中的多个单元格选择

时间:2012-04-20 06:35:06

标签: iphone uitableview ios4 xcode4.3

我需要在tableview中为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 个答案:

答案 0 :(得分:1)

试试此代码。它可以帮助您解决问题

-(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];
    }
}
相关问题