【发布时间】: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