当我滚动表视图时,它删除了所选项 - Objective-c

时间:2016-12-26 12:02:25

标签: ios objective-c uitableview

我是iOS的新手,我在滚动表视图方面遇到问题。当我滚动表视图时,选中的单元格被分割 如图所示,我在第一个单元格上选择P.

enter image description here

但是当我滚动表格时,它会被删除。我正在使用按钮和图像来选择一个按钮。我正在使用自定义表格视图cel。我的代码就像这样

在自定义表格视图单元格

-(IBAction)passbtnClick:(id)sender
{
    Passimage.image =[UIImage imageNamed:@"Pass.png"];
    Failimage.image=[UIImage imageNamed:@"FailGray.png"];
    WIPimage.image =[UIImage imageNamed:@"WIPGray.png"];
    NotApplicableimage.image=[UIImage imageNamed:@"NAGray.png"];
}
-(IBAction)failbtnClick:(id)sender
{
    Passimage.image =[UIImage imageNamed:@"PassGray.png"];
    Failimage.image=[UIImage imageNamed:@"Fail.png"];
    WIPimage.image =[UIImage imageNamed:@"WIPGray.png"];
    NotApplicableimage.image=[UIImage imageNamed:@"NAGray.png"];
}
-(IBAction)wipbtnClick:(id)sender
{
    Passimage.image =[UIImage imageNamed:@"PassGray.png"];
    Failimage.image=[UIImage imageNamed:@"FailGray.png"];
    WIPimage.image =[UIImage imageNamed:@"WIP.png"];
    NotApplicableimage.image=[UIImage imageNamed:@"NAGray.png"];
}
-(IBAction)nabtnClick:(id)sender
{
    Passimage.image =[UIImage imageNamed:@"PassGray.png"];
    Failimage.image=[UIImage imageNamed:@"FailGray.png"];
    WIPimage.image =[UIImage imageNamed:@"WIPGray.png"];
    NotApplicableimage.image=[UIImage imageNamed:@"NA.png"];
}

在viewcontroller.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *STI=@"STI";
    AuditTableViewCell *cell = (AuditTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AuditTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
    cell.audittitlelbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
    cell.auditdesclbl.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
    return cell;
}

我的问题是如何设置它的值,即使我滚动tableview.Thanks in Advance!

2 个答案:

答案 0 :(得分:1)

您的单元格正在重复使用,因此您可以获得混乱的数据。您需要做的是除了idarraynamearray之外还有selectedOption数组,以便您可以相应地填充您的单元格。

此数组的值可以是0,1,2,3,4,关于该值,您可以使用上面列出的方法设置相应的按钮(可以调用-(IBAction)passbtnClick:(id)sender)。

例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        static NSString *STI=@"STI";
        AuditTableViewCell *cell = (AuditTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AuditTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.accessoryType=UITableViewCellAccessoryNone;
        }
        cell.audittitlelbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
        cell.auditdesclbl.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
        cell.selectedButton = [selectedValues objectAtIndex:indexPath.row];
        [cell setupCell];
        return cell;
}

然后,在你的牢房里:

-(void)setupCell {
    if(self.selectedButton == 1){
        [self passbtnClick:self];    
    }
    // repeat for other options, use 0 to have none selected
}

要保存选定状态,请使用从单元格到viewController的委托在selectedButton中设置值selectedValues。对于代表来说,这是一个有用的链接:How do I create delegates in Objective-C?

答案 1 :(得分:0)

创建一个名为cellSelectedArray []的数组,以保留所选单元格的数量。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

  cellSelectedArray = [[NSMutableArrayalloc]init];

//还更新并插入cellSelectedArray中的值等于表中的行数。    //只需将cellSelectedArray中的每个值设置为false。

    }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        static NSString *STI=@"STI";
        AuditTableViewCell *cell = (AuditTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AuditTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.accessoryType=UITableViewCellAccessoryNone;
        }
        if([cellSelectedArray objectAtIndex:indexpath.row] == true){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
        return cell;
} 

//在tableview的select委托中,只需更新数组

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       [cellSelectedArray objectAtIndex:indexpath.row] == true;

}

-(void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       [cellSelectedArray objectAtIndex:indexpath.row] == false;

}