Reputation: 216
There is an array of objects (Zakazchik):
@property (strong, nonatomic) NSMutableArray *zakazchikFavoritesArray;
Each object has field @property int zakazchikCategory
and can vary from 1 to 23 inclusive.
How can I create a TableView, so grouping it was on this field?
ADDED:
in viewDidLoad (after filling 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];
}
also:
-(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];
}
i got error:
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'
Upvotes: 0
Views: 100
Reputation: 17428
The most straightforward way to do this is to first organize your data in to a new data structure that sorts them by the category property. I would recommend using a dictionary that maps from category to an array of all objects that have that category value. You can transform your data like this:
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];
}
Then use this data structure when implementing your UITableView data source methods:
- (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];
}
Upvotes: 1
Reputation: 5684
To avoid error you can rewrite this method as follow
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSMutableArray *objectArray = dataDictionary[@(section)]; // not needed
return [NSString stringWithFormat:@"Category #%i", section];
}
Because your dataDictionary
contains arrays of Zakazchik
not objects directly and, I guess you have another table/array with names of categories so in this method no reason to select this object
Upvotes: 0