简体   繁体   English

使用单个NSMutableArray填充UITableView Sections表

[英]Populate UITableView Sections table with Single NSMutableArray

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. 抱歉再次提出完整说明的问题。我所拥有的resultsArray具有从服务器获取的标题说明等,但问题是我想在各部分中显示此数据。

So lets say if have three sections which are coming from array then how will populate the data in each section using single resultArray. 因此,假设有三个部分来自数组,那么如何使用单个resultArray填充每个部分中的数据。

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

resutArray has all the data for all sections so how to show according to sections resutArray具有所有节的所有数据,因此如何根据节显示

Structure of array is 数组的结构是

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

Look's like you have ObjectData which has attribute called category, and you want to show table view with ObjectData's grouped by categories. 看起来好像您有ObjectData,它的属性称为category,并且您想显示具有按类别分组的ObjectData的表视图。 You may do this generating an NSDictionary with key = unique category, and value = NSArray of ObjectData of that category. 您可以执行此操作,生成一个NSDictionary,其键=唯一类别,值=该类别的ObjectData的NSArray。 Then you use that NSDictionary as a data for datacource. 然后,您可以将该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];
}

Then in your 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];
}

Note that that's just an example for you, you may optimize this as you want. 请注意,这只是一个示例,您可以根据需要对其进行优化。

Using custom class you can achieve this. 使用自定义类可以实现此目的。

myClass.h myClass.h

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

myClass.m myClass.m

#import "myClass.h"

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

First you need to set up your resultArray with above class object. 首先,您需要使用上述类对象设置resultArray。 Set Appropriate value for class. 为类设置适当的值。 Example: 例:

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

Now Set numberOfSectionsInTableView , numberOfRowsInSection , titleForHeaderInSection , cellForRowAtIndexPath 现在设置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;
}

If all you have is an NSMutableArray and you want to split it into sections means you have to know index of objects. 如果您只有NSMutableArray,并且想要将其拆分为多个部分,则意味着您必须了解对象的索引。 For example, 例如,

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

in 1st sections display data from 0 to 2 index, in 2nd from 3 to 5 index, in 3rd from 6 to 8 index. 第1部分显示0到2索引的数据,第2部分3到5索引,第3部分6到8索引。

Else better go for NSDictionary . 否则,最好去NSDictionary Keep section title as key and the values as NSArray in Dictionary. 在字典中,将节标题保留为键,将值保留为NSArray。 So it will be easier to load the cell. 因此,将更容易加载单元。

You have NSArray Of Object Data.. So you can do it as 您有对象数据的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];
    }

So now you will have required data... 所以现在您将拥有所需的数据...

Do same as in the sample code 与示例代码相同

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

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

You might also find it much easier to use the free Sensible TableView framework. 您可能还会发现使用免费的Sensible TableView框架要容易得多。 The framework will simply take your array and generate all the table view cells and sections, including all the detail views if you wish. 该框架将简单地处理您的数组并生成所有表视图单元格和部分,如果需要,还包括所有详细信息视图。 I find it much simpler and literally takes a few minutes to implement. 我发现它要简单得多,实际上需要花费几分钟的时间来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM