0

セクションに表示する必要がある配列がありUITableViewます。

現在、オブジェクトをすべて 1 つのセクションの下に日付順に表示していますが、年ごとにセクションを作成する必要があり、どうすればよいかわかりません。

私のオブジェクトは次のようなものです...

@interface MyEvent : NSObject

@property NSDate *date;
@property NSString *title;
@property NSString *detail;

@end

私の配列は、これらのオブジェクトの日付順の配列です。

この配列から直接これを行うことができますか、それとも配列を 2D 配列に分離する必要がありますか?

つまり、2 番目の NSArray 内の各オブジェクトが同じ年にある NSArray の NSArray。

4

2 に答える 2

1

これは、データ構造としてTLIndexPathToolsTLIndexPathDataModelを使用して簡単に実行できます。ブロックベースの初期化子は、データをセクションに編成する数少ない方法の 1 つを提供します。

NSArray *sortedEvents = ...; // events sorted by date
TLIndexPathDataModel *dataModel = [[TLIndexPathDataModel alloc] initWithItems:sortedEvents sectionNameBlock:^NSString *(id item) {
    MyEvent *event = (MyEvent *)item;
    NSString *year = ...; // calculate section name for the given item from date
    return year;
} identifierBlock:nil];

そして、データ モデル API を使用すると、データ ソース メソッドが非常に簡単になります。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataModel.numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataModel numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = ...;
    UITableViewCell *cell = ...; // dequeue cell
    MyEvent *event = [self.dataModel itemAtIndexPath:indexPath];
    ... // configure cell
    return cell;
}
于 2013-10-15T14:49:47.780 に答える
1

以下のリンクからヘルプが得られる場合があります。

配列内の日付の月ごとの UITableView セクション ヘッダー

http://oleb.net/blog/2011/12/tutorial-how-to-sort-and-group-uitableview-by-date/

于 2013-10-15T12:50:42.770 に答える