gpt4 book ai didi

iphone - 使用单个 NSMutableArray 填充 UITableView 部分表

转载 作者:行者123 更新时间:2023-12-03 20:51:20 24 4
gpt4 key购买 nike

很抱歉再次询问带有完整描述的问题。我拥有的结果数组具有从服务器获取的标题描述等,但问题是我想分段显示这些数据。

因此,假设如果有来自数组的三个部分,那么如何使用单个 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 具有名为类别的属性,并且您想要显示按类别分组的 ObjectData 的 TableView 。您可以执行此操作,生成一个 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];
}

然后在你的 Controller 中:

- (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 部分表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15631115/

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