4

我需要在 iphone 4.3 的 tableview 中选择多行。如果选择不带附件类型,则应更改单元格的背景颜色。导航到多个视图后,如果我返回 table view 需要显示选定的单元格。

我可以进行多项选择,这是我实现的逻辑

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

给按钮动作:

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

谢谢。

4

1 回答 1

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];
    }
}
于 2012-04-20T07:32:06.553 回答