【发布时间】:2011-03-12 11:55:12
【问题描述】:
我有一个NSArray 的对象:
MyObject 包含以下字符串属性:
Title
Category
Name
共有9个类别。
我想在UITableView 中显示这些类别。
目前我的代码看起来像这样,但它并不完全正确,我不确定如何处理我的各个类别
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;` as well.
for (ArrayOfMyObject *item in myArray) {
if ([item.Category isEqualToString:@"Category1"]) {
NSDictionary *dict = [NSDictionary dictionaryWithObjects:item forKey:item.Category];
[self.itemsArray addObject:dict];
}
if ([item.Category isEqualToString:@"Category2"]) {
NSDictionary *dict = [NSDictionary dictionaryWithObjects:item forKey:item.Category];
[self.itemsArray addObject:dict];
}
... (etc, for the rest of the Category titles).
}
如果有 9 个类别,我应该得到总共 9 个部分,但看起来我实际上得到了项目的总数(大约 76 个)。任何帮助将不胜感激,如果需要,请随时请求更多代码。
我想我还需要知道如何在 cellForRowAtIndexPath 方法中处理这个问题。
我会做这样的事情吗?
MyObject *item = (MyObject *)[self.itemsArray objectAtIndex:indexPath.row];
NSMutableDictionary *dictionary = [self.itemsArray objectAtIndex:indexPath.section];
NSArray *categoryArray = [dictionary objectForKey:item.Category];
【问题讨论】:
标签: iphone objective-c nsmutablearray nsdictionary nsmutabledictionary