简体   繁体   English

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

[英]Multiple Cell selection in Tableview for iPhone 4.3

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. 我需要在tableview中为iphone 4.3选择多行。如果选择了带有附件类型,则应该更改单元格的背景颜色。导航到多个视图后,如果我返回到表视图需要显示所选单元格。

I am able to do multiple selection,this is the Logic which i have implemented 我可以做多个选择,这是我实现的逻辑

in cell for Row at IndexPath: 在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];
    }

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. 我正在保存数据库中的选定单元格值,并能够再次返回这些值,返回到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];
    }
}

Thanks. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM