简体   繁体   English

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

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

I am new in iOS and I am facing problem regarding to scroll table view.When I scroll table view selected cell get disselect As shown in the image I select P on the first cell. 我是iOS的新手,在滚动表视图时遇到问题。当滚动表视图时,选定的单元格取消选择。如图所示,在第一个单元格中选择P。

在此处输入图片说明

But when I scroll the table it get removed. 但是,当我滚动表格时,它将被删除。 I am using button and image to select one button at I time.I am using custom table view cel. 我正在使用按钮和图像来一次选择一个按钮。我正在使用自定义表格视图cel。 My code is like this 我的代码是这样的

In Custom Table View cell 在“自定义表格视图”单元格中

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

In viewcontroller.m 在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;
}

My question is how to set it value even I scroll tableview.Thanks in Advance! 我的问题是即使我滚动tableview也要如何设置它的值。

Your cells are being reused so you get messed data. 您的单元正在重复使用,因此您会弄乱数据。 What you need to do is besides idarray and namearray to have selectedOption array so you can populate your cell accordingly. 您需要做的是除了idarraynamearray之外还有selectedOption数组,以便可以相应地填充单元格。

This array can have values 0, 1, 2, 3, 4 and regarding the value you can set appropriate button selected with above methods you listed ( you can call -(IBAction)passbtnClick:(id)sender ). 该数组的值可以为0、1、2、3、4,关于​​该值,可以设置使用上面列出的方法选择的适当按钮(可以调用-(IBAction)passbtnClick:(id)sender )。

For instance : 例如 :

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

And then, in your cell: 然后,在您的单元格中:

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

For saving selected state, use delegate from cell to viewController to set value selectedButton in selectedValues . 为了节省选择状态时,使用代表从小区到的viewController设定值selectedButtonselectedValues For delegates, here is the useful link: How do I create delegates in Objective-C? 对于代表,这是有用的链接: 如何在Objective-C中创建代表?

Create an array called cellSelectedArray[] to keep a count of selected cells. 创建一个名为cellSelectedArray []的数组以保留所选单元格的数量。

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

  cellSelectedArray = [[NSMutableArrayalloc]init];

// also update and insert values in cellSelectedArray equal to number of rows in table. //还可以在cellSelectedArray中更新并插入等于表中行数的值。 // just set every value in cellSelectedArray to be false. //只需将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;
} 

//in select delegate of tableview , just update the array //在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