-1

私はそれを検索しようとしましたが、今のところ答えが見つからないようです。

現在、プロトタイプ セルを使用してデータを動的に入力しています。

日付に従ってセルを動的にグループ化する必要があります。

2001 年 1 月 1 日に 3 つの行があるとしましょう。2001 年 2 月 1 日の時点で、5 行あります。セルを動的にグループ化する方法を示すガイドまたはサンプルが見つからないようです。以下は私のコードの一部です。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 #warning Potentially incomplete method implementation.
 // Return the number of sections.
 return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [someArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
static NSString *CellIdentifier = @"cell";
IssuesViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[IssuesViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];
}

// Configure the cell...  
currentSomething = [[Something alloc] init];
currentSomething = [somethingDiscovered objectAtIndex:indexPath.row];

cell.typeLabel.text = currentSomething.type;
cell.titleLabel.text = currentSomething.title;
cell.begDateLabel.text = currentSomething.begDate;

return cell;
}

2013 年 8 月 23 日更新:

動的にグループ化できるようになりましたが、セルの正しいデータを表示する際に問題が発生しています。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 #warning Potentially incomplete method implementation.
 // Return the number of sections.
 return return [someDate count]; //someDate is an array that stores dates
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// set title of section here
return [someDate objectAtIndex:section]; //set the title of each section as a date
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [[someIssues objectAtIndex:section] count]; //some issues hold data to be displayed in the cell.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
static NSString *CellIdentifier = @"cell";
IssuesViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[IssuesViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];
}

// Configure the cell...  
currentSomething = [[Something alloc] init];
currentSomething = [somethingDiscovered objectAtIndex:indexPath.row];

cell.typeLabel.text = currentSomething.type;
cell.titleLabel.text = currentSomething.title;
cell.begDateLabel.text = currentSomething.begDate;

return cell;
}

たとえば、オブジェクト A、B、C、D、E、F、G、H、I、J、K。そして、4 つのセクションがあります。section[0] は A、B、C、D、E、F、G を表示するとします。 Section[3] は、H、I、J、K を表示するとします。

しかし今、セクション[0]にはA、B、C、D、E、F、Gしか表示されません。A、B、C、D、セクション [3]。手伝ってください。

解決:

someIssue = [[someIssues objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
4

1 に答える 1

2

これを行う1つの方法は、テーブルビューにデータを入力する構造を持ち、それが変更されるたびに、テーブルビューのすべてまたはその一部だけをリロードすることです。

– reloadData //Reload all of your table view's data
– reloadRowsAtIndexPaths:withRowAnimation: //Reload some rows
– reloadSections:withRowAnimation: //Reload some defined sections

これらのメソッドの使用方法の詳細については、UITableView クラス リファレンスを参照してください。

構造は次のように単純にすることができます。

 //This will represent two sections with two cells each
`NSArray *contentArray = @[@[@"Cell A",@"Cell B"],@[@"Cell C",@"Cell D"]];` 

これは、セクションを表す要素を含む配列です。各要素/セクションは、すべてのセル値を含む配列でもあります。

そして、TableView の Delegate および Datasource メソッドでは、これを行う必要があります

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (contentArray) {
        return contentArray.count;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (contentArray) {
        return [[contentArray objectAtIndex:section] count];
    }
    return 0;
}
于 2013-08-21T20:56:56.603 に答える