【问题标题】:Multiple Cell selection in Tableview for iPhone 4.3iPhone 4.3 的 Tableview 中的多个单元格选择
【发布时间】:2012-04-20 06:35:05
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: iphone uitableview ios4 xcode4.3


    【解决方案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];
        }
    }
    

    【讨论】:

    • 但如果我想取消选择单元格。它不会发生。
    • 取一些布尔值并将其放在需要的地方
    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多