使用Single NSMutableArray填充UITableView Sections表 [英] Populate UITableView Sections table with Single NSMutableArray

查看:93
本文介绍了使用Single NSMutableArray填充UITableView Sections表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Sorry to ask again the question with full description.What i have that i have resultsArray which has title description etc which gets from server but problem is that i want to show this data in sections.

因此,假设有三个部分来自数组,那么如何使用单个resultArray填充每个部分中的数据。

So lets say if have three sections which are coming from array then how will populate the data in each section using single resultArray.

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
return [categoryArray objectAtIndex:section];
}

resutArray包含所有部分的所有数据,以便如何根据部分显示

resutArray has all the data for all sections so how to show according to sections

数组的结构是

             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按类别分组。
您可以使用key = unique类别生成NSDictionary,并且value =该类别的ObjectData的NSArray。然后使用该NSDictionary作为数据源的数据。

Look's like you have ObjectData which has attribute called category, and you want to show table view with ObjectData's grouped by categories. You may do this generating an NSDictionary with key = unique category, and value = NSArray of ObjectData of that category. Then you use that NSDictionary as a data for datacource.

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

请注意,这只是一个例子,您可以根据需要对其进行优化。

Note that that's just an example for you, you may optimize this as you want.

这篇关于使用Single NSMutableArray填充UITableView Sections表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆