簡體   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