gpt4 book ai didi

ios - 在 TableView 中分组

转载 作者:行者123 更新时间:2023-11-29 01:59:09 24 4
gpt4 key购买 nike

有一个对象数组(Zakazchik):

@property (strong, nonatomic) NSMutableArray *zakazchikFavoritesArray;

每个对象都有字段 @property int zakazchikCategory,可以从 1 到 23 不等。

如何创建一个 TableView,以便在该字段上对其进行分组?

添加:

在 viewDidLoad 中(填充 zakazchikFavoritesArray 后):

dataDictionary = [NSMutableDictionary dictionary];

for (Zakazchik *object in zakazchikFavoritesArray) {
NSMutableArray *categoryArray = dataDictionary[@(object.zakazchikCategory)];
if (categoryArray == nil) {
categoryArray = [NSMutableArray array];
dataDictionary[@(object.zakazchikCategory)] = categoryArray;
}
[categoryArray addObject:object];
}

还有:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return dataDictionary.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *array = dataDictionary[@(section)];
return array.count;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Zakazchik *object = dataDictionary[@(indexPath.section)][@(indexPath.row)];

static NSString *CellIdentifier = @"ZakazchikCellFavorites";
ZakazchikCellFavorites *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

customCell.zakazchikName.text = object.zakazchikName;

return customCell;
}


-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Zakazchik *object = dataDictionary[@(section)];

return [NSString stringWithFormat:@"%i", object.zakazchikCategory];
}

我得到错误:

2015-05-30 myApp[12521:484566] -[__NSArrayM zakazchikCategory]: unrecognized selector sent to instance 0x7b735f00
2015-05-30 myApp[12521:484566] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM zakazchikCategory]: unrecognized selector sent to instance 0x7b735f00'

最佳答案

最直接的方法是首先将您的数据组织到一个新的数据结构中,该结构按类别属性对它们进行排序。我建议使用从类别映射到具有该类别值的所有对象的数组的字典。您可以像这样转换数据:

NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionary];

for (Zakazchik *object in zArray) {
NSMutableArray *categoryArray = dataDictionary[@(object.category)];
if (categoryArray == nil) {
categoryArray = [NSMutableArray array];
dataDictionary[@(object.zakazchikCategory)] = categoryArray;
}
[categoryArray addObject:object];
}

然后在实现 UITableView 数据源方法时使用此数据结构:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return dataDictionary.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *array = dataDictionary[@(section)];
return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Zakazchik *object = dataDictionary[@(indexPath.section)][@(indexPath.row)];

// Create your cell using the object..
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *array = dataDictionary[@(section)];
Zakazchik *firstObject = [array firstObject];

return [NSString stringWithFormat:@"%i", firstObject.zakazchikCategory];
}

关于ios - 在 TableView 中分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30543277/

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