0

おそらくこれに対する簡単な答えがありますが、私はそれを見つけることができません。

NSDictionary オブジェクトで満たされた NSArray があります。各 NSDictionary オブジェクト内のオブジェクト数/2 によって、UITableviewcell の各セクションの行数が決まります。同様に、NSDictionary オブジェクトの数によって、セクションの数が決まります (これは単純に [NSArray カウント] です)。

基本的な例:

self.myArray = [NSArray arrayWithObects:
[NSDictionary dictionaryWithObjectsAndKeys:obj1, key1, obj2, key2,..., nil],
...
[NSDictionary dictionaryWithObjectsAndKeys:objA, keyA ... objn, keyn, nil],
nil];

私が行っていることは、すべての NSDictionary 内のオブジェクトの数を手動で数え、各数値を 2 で割り、その情報を使用して、各要素にセクションごとの行数が含まれる配列を作成することです。その手動で作成された配列は、tableview numberOfRowsInSection デリゲートで使用されます。これはせいぜい粗雑に思えます。ちなみに、重要ではありませんが、2 で割った理由は、各テーブルビュー セルが 2 つの UIlabel サブビューで構成されているためです。

よろしくお願いします。

4

2 に答える 2

1

NSArrayおよびNSDictionarycountで関数を使用します。

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dictionary = [myArray objectAtIndex:section];
    return [dictionary count] / 2;
}
于 2012-06-24T06:53:14.783 に答える
1
@implementation NSArray (totalObjects)

- (NSUInteger) totalObjects
{
NSUInteger result = 0;
for (id object in self)
 result += [object isKindOfClass:[NSDictionary class]] ? [[object allKeys] count] : 1;
return result;
}

@end
于 2012-06-24T06:54:41.087 に答える