0

以下のコードを使用して、ディストリビューターごとにテーブルビューを並べ替えています(製品ごとにアルファベット順でした)

    NSSortDescriptor *aSort =[[NSSortDescriptor alloc] initWithKey:@"Dis" ascending:YES];
    [distribArray sortUsingDescriptors:[NSMutableArray arrayWithObject:aSort]];

    NSLog( @"data from table %@", distribArray);

    [self.tableView reloadData];


    NSLog(@"ok2222222222");
    [[NSUserDefaults standardUserDefaults] setValue:@"Dis" forKey:@"ListBy"];
    [[NSUserDefaults standardUserDefaults] synchronize];

そのディストリビューターのすべての製品の上に、タイトルヘッダーとしてディストリビューター名を表示する最も簡単な方法を知りたいです。現在、各製品のセルのDetailsViewにディストリビューター名を表示しています。

から行きたいです。

Product 1
Acme
Product 2
Acme
Product 3
Acme

以下にこれを行い、UITableView\Cellsを保持します

Acme
Product 1
Product 2
Product 3

....助けてくれてありがとう。

4

1 に答える 1

0

最速の方法ではないかもしれませんが、簡単だと思います

まず、次のような小さな内部クラスを作成します。

@interface ProductSection
@property (strong, nonatomic) NSString* sectionName;
@property (strong, nonatomic) NSMutableArray* products;
@end

次に、ソートの代わりにこれを使用します。

NSSortDescriptor *aSort =[[NSSortDescriptor alloc] initWithKey:@"Dis" ascending:YES];
NSArray* products = [distribArray sortUsingDescriptors:[NSMutableArray arrayWithObject:aSort]];

self.sections = [NSMutableArray array];

NSString* currentDistributor = nil;
for (Product* p in products) {
    if (![p.Dis isEqualToString:currentDistributor]) {
        ProductSection* section = [[ProductSection alloc] init];
        section.sectionName = p.Dis;
        section.products = [NSMutableArray array];
        [self.sections addObject:section];

        currentDistributor = p.Dis;
    }
    ProductSection* section = [self.sections lastObject];
    [section.products addObject:p];

}
[self.tableView reloadData];

self.sectionsの可変配列はどこにありますかProductSection

次にあなたのでこれを使用してくださいTable View Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[self.sections objectAtIndex:section] products] count];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.sections count];

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
[[self.sections objectAtIndex:section] sectionName];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Product* p = [[[self.sections objectAtIndex:indexPath.section] products] objectAtIndex:indexPath.row];
...

}

それが役立つことを願っています

于 2012-10-12T21:29:59.233 に答える