【问题标题】:Populate UITableView Sections table with Single NSMutableArray使用单个 NSMutableArray 填充 UITableView Sections 表
【发布时间】:2013-03-15 21:31:34
【问题描述】:

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

假设如果有三个部分来自数组,那么如何使用单个 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];

【问题讨论】:

  • 请您展示一下您的数组的结构,您在其中存储了哪些数据。以及要在部分中显示的项目。
  • @DipenPanchasara 我已经添加了数组的结构
  • 显示你的代码然后很容易解决..谢谢
  • 你可以计算一行对象的数量,然后从下一个开始。这有那么难吗?
  • @AnoopVaidya 我不明白你能解释更多

标签: iphone ipad nsmutablearray uitableview


【解决方案1】:

看起来你有 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];
}

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

【讨论】:

  • 是的,你是对的,我想要这样,如果你能添加一些代码,请添加一些代码
  • 我认为我们必须提供从 json 获取结果的 resultArray,而不是 categoires
  • 它给出了错误 index.path undeclared in numberofRowsInSection
  • 只是一个错字,已修复。我这里是在网页界面写代码,所以这里可能会遇到一些错别字和小错误。
  • 很好的答案!应该标记为正确答案,它对我帮助很大
【解决方案2】:

使用自定义类可以实现这一点。

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

【讨论】:

  • 但是它将如何为不同标题的数据分隔每个部分
  • 你说得对,可以说我在数组中有标题部分,resultArray 包含所有数据,它将如何将每个部分的数据与 resultArray 分开
  • 按照我所说的查看我的 resultArray 和 categoryArray 这是部分数组标题
  • 您的 returnArray 拥有所有部分的所有数据。现在您需要将类型为 NSMutableArray 的对象设置为您的 ObjectData 类。并且您可以访问该数组和值,如上所示
  • 给什么 myClassObje.sectionTitle=@"Your Section Title" 这来自 categoryArray
【解决方案3】:

如果你只有一个 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。在 Dictionary 中将部分标题保留为键,并将值保留为 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];
    }

所以现在您将获得所需的数据...

【讨论】:

  • 是的部分标题最好是制作密钥和加载数据,例如如何做到这一点
  • 查看更新的答案。您已根据索引拆分数据,并且所有部分都应该有单独的键...
  • 我没有得到这个你可以添加我的代码,因为我已经更清楚了请
【解决方案4】:

执行与示例代码相同的操作

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

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

【讨论】:

  • 我不知道确切的节数,有时可能比 10 多或少 10,因为数组来自服务器
【解决方案5】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多