简体   繁体   中英

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.

在此处输入图片说明

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. 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

- (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!

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.

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 ).

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 . For delegates, here is the useful link: How do I create delegates in Objective-C?

Create an array called cellSelectedArray[] to keep a count of selected cells.

- (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. // just set every value in cellSelectedArray to be 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

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

}

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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