抱歉再次提出完整说明的问题。我所拥有的resultsArray具有从服务器获取的标题说明等,但问题是我想在各部分中显示此数据。
因此,假设有三个部分来自数组,那么如何使用单个resultArray填充每个部分中的数据。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [categoryArray objectAtIndex:section];
}
resutArray具有所有节的所有数据,因此如何根据节显示
数组的结构是
ObjectData *theObject =[[ObjectData alloc] init];
[theObject setCategory:[dict objectForKey:@"category"]];
[theObject setSub_Category:[dict objectForKey:@"sub_Category"]];
[theObject setContent_Type:[dict objectForKey:@"content_Type"]];
[theObject setContent_Title:[dict objectForKey:@"content_Title"]];
[theObject setPublisher:[dict objectForKey:@"publisher"]];
[theObject setContent_Description:[dict objectForKey:@"content_Description"]];
[theObject setContent_ID:[dict objectForKey:@"content_ID"]];
[theObject setContent_Source:[dict objectForKey:@"content_Source"]];
[resultArray addObject:theObject];
最佳答案
看起来好像您有ObjectData,它的属性称为category,并且您想显示具有按类别分组的ObjectData的表视图。
您可以这样做来生成一个NSDictionary,其键=唯一类别,值=该类别的ObjectData的NSArray。然后,您可以将该NSDictionary用作数据源的数据。
NSArray *tempArray =[[DataController staticVersion] startParsing:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/catalogMaster.php"];
// dictionary for cells, you'll probably want this to be ivar or property
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
// array for headers, you'll probably want this to be ivar or property
NSMutableArray* categories = [NSMutableArray array];
for (int i = 0; i<[tempArray count]; i++) {
id *item = [tempArray objectAtIndex:i];
NSDictionary *dict = (NSDictionary *) item;
ObjectData *theObject =[[ObjectData alloc] init];
[theObject setCategory:[dict objectForKey:@"category"]];
[theObject setSub_Category:[dict objectForKey:@"sub_Category"]];
[theObject setContent_Type:[dict objectForKey:@"content_Type"]];
[theObject setContent_Title:[dict objectForKey:@"content_Title"]];
[theObject setPublisher:[dict objectForKey:@"publisher"]];
[theObject setContent_Description:[dict objectForKey:@"content_Description"]];
[theObject setContent_ID:[dict objectForKey:@"content_ID"]];
[theObject setContent_Source:[dict objectForKey:@"content_Source"]];
[resultArray addObject:theObject];
[theObject release];
theObject=nil;
// here you populate that dictionary and categories array
NSMutableArray* objs = [dictionary objectForKey:theObject.category];
if(!objs)
{
objs = [NSMutableArray array];
[dictionary setObject:objs forKey:theObject.category];
[categories addObject:theObject.category];
}
[objs addObject:theObject];
}
然后在您的控制器中:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [categories count] ;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [categories objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
NSString* category = [categories objectAtIndex: indexPath.section];
NSMutableArray* objs = [dictionary objectForKey:category];
ObjectData* obj = [objs objectAtIndex: indexPath.row];
// populate cell here
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString* category = [categories objectAtIndex:section];
NSMutableArray* objs = [dictionary objectForKey:category];
return [objs count];
}
请注意,这只是一个示例,您可以根据需要对其进行优化。
关于iphone - 使用单个NSMutableArray填充UITableView Sections表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15631115/