繁体   English   中英

当我滚动表格视图时,它删除了选定的项目-Objective-c

[英]When I scroll table view it removed selected items - Objective-c

我是iOS的新手,在滚动表视图时遇到问题。当滚动表视图时,选定的单元格取消选择。如图所示,在第一个单元格中选择P。

在此处输入图片说明

但是,当我滚动表格时,它将被删除。 我正在使用按钮和图像来一次选择一个按钮。我正在使用自定义表格视图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也要如何设置它的值。

您的单元正在重复使用,因此您会弄乱数据。 您需要做的是除了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设定值selectedButtonselectedValues 对于代表,这是有用的链接: 如何在Objective-C中创建代表?

创建一个名为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的选择委托中,只需更新数组

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

}

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

}

暂无
暂无

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

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