3

動的データを含むテーブルを作成しようとしていますが、行き詰まっています。これが私のデータの構造です:

NSMutableArray *bigArray;

bigArrayには多くのNSDictionaryアイテムがあります。

それぞれitemsにエントリが 1 つだけあります。

sectionNameがキー、NSMutableArray が値です。

valueNSMutableArrayには多くのオブジェクトがあります。

これをできるだけ簡単に説明しようとしましたが、ここで行き詰まった部分です。

//easy
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [bigArray count];
}

//medium
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
    // Return the number of rows in the section.
    return [[[[bigArray objectAtIndex:section] allValues] objectAtIndex:0] count];
}

現在のデータ構造に基づいてこのメソッドを実装する方法をこの部分で理解できません:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView 
                             dequeueReusableCellWithIdentifier:@"MyCell"];

    MyObject *obj = //Need this part


    cell.textLabel.text = obj.name;   

    return cell;

}

簡単に言えば、動的データを含む動的セクションを挿入しようとしています。経験豊富な開発者からのアドバイスを探しています。これにどのように取り組みますか?

4

1 に答える 1

0

あなたのデータがどのように構造化されているかをよく理解していると仮定すると、次のようになります。

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    }

    //this will get you the dictionary for the section being filled
    NSDictionary *item = [bigArray objectAtIndex:indexPath.section];
    // then the array of object for the section
    NSMutableArray *mutableArray = [item objectForKey:@"sectionName"];
    //you then take the object for the row
    MyObject *obj = [mutableArray objectAtIndex:indexPath.row];

    cell.textLabel.text = obj.name;   

    return cell;
}

属性インスペクタでセル プロトタイプの再利用識別子を設定することを忘れないでください

于 2012-04-21T21:17:48.987 に答える