使用Single NSMutableArray填充UITableView Sections表

时间:2013-03-26 06:56:59

标签: iphone ipad nsmutablearray uitableview

很抱歉再次询问完整描述的问题。我有什么,我有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];

5 个答案:

答案 0 :(得分:2)

看起来就像你有ObjectData这个属性叫做类别,你想要显示表格视图,其中ObjectData按类别分组。 您可以使用key = unique category生成NSDictionary,并使用该类别的ObjectData的value = 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];
}

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

答案 1 :(得分:1)

使用自定义类,您可以实现此目的。

<强> myClass.h

@interface myClass : NSObject
@property(nonatomic,retain) NSString* sectionHeader;
@property(nonatomic,retain) NSMutableArray* sectionRows;
@end

<强> myClass.m

#import "myClass.h"

@implementation myClass
@synthesize sectionHeader;
@synthesize sectionRows;
@end

首先,您需要使用上面的类对象设置resultArray。为类设置适当的值。例如:

myClass *myClassObj = [[myClass alloc] init];
myClassObj.sectionHeader = @"Your section title";
myClassObj.sectionRows   = < Your NSMutableArray for rows under current section >;
[retunArray addObject:myClassObj];

现在设置numberOfSectionsInTableViewnumberOfRowsInSectiontitleForHeaderInSectioncellForRowAtIndexPath

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [returnArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[returnArray objectAtIndex:section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    myClass *myClassObject = [returnArray objectAtIndex:section];
    return myClassObject.sectionHeader;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    myClass *myClassObject = [returnArray objectAtIndex:indexPath.section];
    NSLog(@"%@",[myClassObject.sectionRows objectAtIndex:indexPath.row]);

    return cell;
}

答案 2 :(得分:0)

如果您拥有的是NSMutableArray并且想要将其拆分为多个部分意味着您必须知道对象的索引。 例如,

[
Section 1,
Section1,
Section1,
Section 2,
Section 2,
Section2,
Section 3,
Section 3,
Section 3
]

在第1部分显示从0到2索引的数据,在第2个从3到5索引,在第3个从6到8索引。

否则最好去NSDictionary。将节标题保留为键,将值保存为字典中的NSArray。因此加载单元格会更容易。

你有对象数据的NSArray ..所以你可以这样做

    NSMutableDictionary *sectionDict = [[NSMutableDictionary alloc] init];

    for(int i=0; i < [data count]; i++)
    {
ObjectData *theObject = [data objectAtIndex:i];
        NSMutableArray *arr;
        if([sectionDict valueForKey:theObject.category])
        {
            arr = [sectionDict valueForKey:theObject.category];
        }
        else
        {
            arr = [[NSMutableArray alloc] init];
        }
        [arr addObject:theObject];
        [sectionDict setObject:arr forKey:theObject.category];
    }

所以现在你需要数据......

答案 3 :(得分:0)

与示例代码中的相同

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [sectionArray count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
 //Do your stuff
} 

答案 4 :(得分:0)

您可能还会发现使用免费的Sensible TableView框架要容易得多。框架将简单地获取您的数组并生成所有表视图单元格和节,如果您愿意,还包括所有详细视图。我发现它更简单,实际上需要几分钟才能实现。

相关问题