在TableView中分组

时间:2015-05-30 06:56:46

标签: ios objective-c uitableview

有一系列对象(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'

2 个答案:

答案 0 :(得分:1)

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

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];
}

答案 1 :(得分:0)

为避免错误,您可以按照以下步骤重写此方法

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   NSMutableArray *objectArray = dataDictionary[@(section)]; // not needed 
   return [NSString stringWithFormat:@"Category #%i", section];
}

因为你的dataDictionary包含Zakazchik数组而不是直接对象,我猜你有另一个表/数组,其中包含类别名称,所以在这种方法中没有理由选择这个对象