gpt4 book ai didi

ios - 如何在 TableView 中的可变数组中存储多个选定的行项

转载 作者:行者123 更新时间:2023-11-28 21:10:03 25 4
gpt4 key购买 nike

我已经编写了用于显示包含 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);

}
}

最佳答案

  1. 首先为数组创建一个导出并在 .m 中合成

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

}

关于ios - 如何在 TableView 中的可变数组中存储多个选定的行项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43907986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com