2

完全な説明でもう一度質問して申し訳ありません。サーバーから取得したタイトルの説明などを持つresultsArrayがありますが、問題はこのデータをセクションに表示したいということです。

したがって、配列から来る 3 つのセクションがある場合、単一の 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];
4

5 に答える 5

2

カテゴリと呼ばれる属性を持つ ObjectData があり、カテゴリ別にグループ化された ObjectData を含むテーブル ビューを表示したいとします。これを行うには、キー = 一意のカテゴリ、値 = そのカテゴリの ObjectData の NSArray で NSDictionary を生成します。次に、その 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];
}

これは単なる例であることに注意してください。必要に応じてこれを最適化できます。

于 2013-03-26T08:39:56.197 に答える
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];

今セットnumberOfSectionsInTableView, numberOfRowsInSection, titleForHeaderInSection,cellForRowAtIndexPath

- (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;
}
于 2013-03-26T07:11:00.537 に答える
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];
    }

これで、必要なデータが得られます...

于 2013-03-26T07:10:33.860 に答える
0

サンプルコードと同じようにする

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
 //Do your stuff
} 
于 2013-03-26T08:34:11.837 に答える
0

また、無料の Sensible TableView フレームワークを使用する方がはるかに簡単であることに気付くかもしれません。フレームワークは単に配列を取得し、必要に応じてすべての詳細ビューを含む、すべてのテーブル ビュー セルとセクションを生成します。私はそれがはるかに簡単で、文字通り数分で実装できると思います。

于 2013-03-26T09:16:58.700 に答える