我有一个分组的UITableview,我用一个JSON对象填充它。某些部分/组需要至少选中一行。每次点击时,我都会向 NSMutableDictionary 添加一个键/值来构造我选择的数组。现在在IBAction上,我需要检查每个"必需"组是否都选择了某些内容。我知道我的 json 对象需要哪些部分来指示它。
我的想法是从我的新数组中获取所有 ID,看看每个"必需"组是否在"主"NSDictionary 中至少有一个键,尽管我不知道这是否是正确的想法
一些示意图可以提供帮助:
ns字典中的"主"json 对象
{
"category_id": "2",
"category_required": "1",
"category_items": [
{
"id": "1",
"categoryid": "2",
"title": "Pie",
"price": "1.20",
"offer_price": null,
"can_have_multi": "0"
},
{
"id": "2",
"categoryid": "2",
"title": "Tea",
"price": "1.50",
"offer_price": null,
"can_have_multi": "0"
},
等。。。
"选定项目"NS词典:
"ingredients" : {
"8" : {
"multi" : "1"
},
"13" : {
"multi" : "1"
},
"12" : {
"multi" : "1"
},
"2" : {
"multi" : "1"
}
},
等。。。
因此,这个想法是在检查哪些"类别"具有必需的选择后,将第二个数组的每个 id 与主 nsdictionary id 进行比较。
有意义吗?任何有助于我走上正轨的提示都会有很大帮助
我可能会将数据映射到提供更简单方法来验证它的对象。
对象映射...
@interface Category : NSObject
@property (strong) NSString *categoryID;
@property BOOL *selected;
// Etc... all the category properties
@end
@interface CategoryGroup : NSObject
@property (strong) NSString *group_id;
@property (strong) NSNumber *items_required;
@property (strong) NSArray *categories;
- (BOOL)enoughCategoriesSelected
@end
@implementation CategoryGroup
- (BOOL)enoughCategoriesSelected
{
NSInteger selected = 0;
for (Category *cat in self.categories)
{
if (cat.selected)
selected++;
}
return (selected >= self.items_required.intValue);
}
@end
表视图的东西...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// .....
CategoryGroup *group = self.groups[indexPath.section];
Category *cat = group.categories[indexPath.row];
if (cat.selected)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
// ....
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
CategoryGroup *group = self.groups[indexPath.section];
Category *cat = group.categories[indexPath.row];
cat.selected = !cat.selected;
[self.tableView reloadData];
}
最后。。。
- (IBAction)validateCategoryGroups
{
for (CategoryGroup *group in self.groups)
{
if ([group enoughCategoriesSelected])
// all good...
else
// no good...
}
}