我有下面的原始代码,可以正常工作:-

- (ZYSideSlipFilterRegionModel *)commonFilterRegionModelWithKeyword:(NSString *)keyword selectionType:(CommonTableViewCellSelectionType)selectionType {
    ZYSideSlipFilterRegionModel *model = [[ZYSideSlipFilterRegionModel alloc] init];
    model.containerCellClass = @"SideSlipCommonTableViewCell";
    model.regionTitle = keyword;
    model.customDict = @{REGION_SELECTION_TYPE:@(selectionType)};
    model.itemList = @[[self createItemModelWithTitle:[NSString stringWithFormat:@"Local"] itemId:@"0" selected:NO],
    [self createItemModelWithTitle:[NSString stringWithFormat:@"Oversea"] itemId:@"1" selected:NO]];
    return model;
}


现在,我计划将静态值(海外/本地)更改为动态值。但是将仅显示1个项目。

for (int i = 0; i < filteredArray.count; i++) {
    int intItemID = i + 1;
    NSString *myNewString = [NSString stringWithFormat:@"%i", intItemID];
    model.itemList = @[[self createItemModelWithTitle:[filteredArray[i] valueForKey:@"attribute_name"] itemId:myNewString selected:NO] ];
}


如何将2个项目放入model.itemList中?请帮忙。谢谢。

最佳答案

你可以用这种方式

// step:1像这样获取字典

   for (int i = 0; i < filteredArray.count; i++)
{

    NSMutableDictionary *dict = (NSMutableDictionary *)filteredArray[i] ;
    int intItemID = i + 1;
    NSString *myNewString = [NSString stringWithFormat:@"%i", intItemID];
    model.itemList = @[[self createItemModelWithTitle:dict];
}


//步骤2:您可以像这样定义Model的方法

- (CommonItemModel *)createItemModelWithTitle:(NSMutableDictionary *)dictModel
 {

    CommonItemModel *model = [[CommonItemModel alloc] init];
    model.itemId = [dictModel valueForKey : @"itemId"];
    model.itemName = [dictModel valueForKey:@"itemTitle"];
    model.selected = [dictModel valueForKey:[NSNumber numberWithBool:
    [[dictModel valueForKey:@"selected"]]]];

    return model;

 }


//第一步要做的另一件事是

    model.itemList = @[[self createItemModelWithTitle:dict];

But the method only return the Model class (CommonItemModel) so if you need any help you shared here


谢谢 :)

关于ios - 如何使用数组方法在模型中添加多个项目?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53515621/

10-14 23:11