【问题标题】:How to store multiple selected row items in mutable array in tableview如何在tableview的可变数组中存储多个选定的行项
【发布时间】:2025-11-21 19:25:02
【问题描述】:

我已经编写了用于显示具有 3 个部分的 tableview 的代码,现在我想将选定的行项存储到一个数组中,当我尝试下面的代码时,我得到的数组值为 nil。建议我如何解决这个问题 下面我添加代码和 Json 示例数据

{
  "technicians": {
    "group": [
      {
        "first_name": "First",
        "last_name": "Available",
        "id": ""
      },
      {
        "first_name": "idea",
        "last_name": "Chandra",
        "id": 2885
      },
      {
        "first_name": "manson",
        "last_name": "Tolios",
        "id": 2878
      },
      {
        "first_name": "tata",
        "last_name": "Masson",
        "id": 2869
      }
    ],
    "client": [
      {
        "first_name": "tsms",
        "last_name": "ysys",
        "id": 2875
      },
      {
        "first_name": "Oscar",
        "last_name": "tata",
        "id": 2851
      },
      {
        "first_name": "Peter",
        "last_name": "yaha",
        "id": 2873
      },
      {
        "first_name": "iaka",
        "last_name": "adad",
        "id": 2847
      },
      {
        "first_name": "taga",
        "last_name": "uaja",
        "id": 2917
      }
    ],
    "contractors": [
      {
        "first_name": "taha"
      },
      {
        "first_name": "haka"
      },
      {
        "first_name": "oala"
      }
    ]
  }
}

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

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"EcsStafflist"];
        if(!cell)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
    
        }
        UILabel *Fname = (UILabel *)[cell viewWithTag:18];
    
        UILabel *Lname = (UILabel *)[cell viewWithTag:19];
    
        Fname.text = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]valueForKey:@"first_name"];
    
        Lname.text = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]valueForKey:@"last_name"];
    
        if (indexPath.row %2 ==1)
    
            cell.backgroundColor = [UIColor colorWithRed:0.60 green:0.60 blue:0.95 alpha:1.0];
    
        else
    
            cell.backgroundColor = [UIColor colorWithRed:0.64 green:0.89 blue:0.61 alpha:1.0];

        return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  stringId = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]objectForKey:@"id"];

    if
        ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        
    }else{
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

        [myArray addObject:stringId];

        NSLog(@"myArray %@",myArray);
        
    }
}

【问题讨论】:

  • 你初始化 myArray 了吗?
  • 是的,我已经初始化了@hamentmiglani
  • 你在哪里得到零?如果您在初始化数组时遇到问题,那么这与 tableview 函数无关。您是否确认您的数组已正确填充?
  • 在 ns 日志中我得到我的数组是空的,是的,我的数组被正确填充了@Russell
  • 不能为 nil 且正确填充!

标签: ios objective-c iphone json uitableview


【解决方案1】:
  1. 首先在.m中为数组创建一个outlet并合成

  2. 现在在 viewdidLoad 中初始化数组

  3. 现在实现 cellforrowatIndexpath 和 didSelectRowAtIndexPath

    找到代码,如果您需要澄清,请告诉我

String_Name = [ArrayName objectAtIndex:indexPath.row];

    if
        ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        
    }else{
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

        [myArray addObject:String_Name];

        NSLog(@"myArray %@",myArray);
        
    }

【讨论】:

    【解决方案2】:

    试试这个代码

    在创建新的NSMubaleArray *storeArray; NSMutableArray 之前,在viewDidLoad 中分配storeArray = [[NSMutableArray alloc]init];

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        switch (indexPath.section) {
            case 0:
                if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
                {
                    [storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
                }else{
                [storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
                }
                break;
            case 2:
                if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
                {
                    [storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
                }else{
                    [storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
                }
                break;
            case 3 :
                if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
                {
                    [storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
                }else{
                    [storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
                }
                break;
            default:
                break;
        }
    
        NSLog(@"%@",storeArray);
    }
    

    【讨论】:

      最近更新 更多