テーブルビューに読み込まれる配列があり、ユーザーが特定のセルをタップすると、UITableViewCellAccessoryCheckmarkに変わります。配列内のどのオブジェクトがチェックされているかを確認し、チェックされているすべてのオブジェクトを別の配列に追加するにはどうすればよいですか?
2 に答える
1
あなたのtableView:didSelectRowAtIndexPath:
方法でこのようなもの:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//set checkmark accessory on table cell ...
// get object and add to checkedObjects array
NSInteger index = [indexPath row];
MyObject *object = [myArray objectAtIndex:index];
[checkedObjects addObject:object];
}
于 2011-09-01T00:18:32.980 に答える
1
チェックされたオブジェクトを気まぐれに実際に取得する関数が必要な場合は、次を使用します。
- (NSMutableArray*)checkedObjectsInTable:(UITableView*)tableView
{
NSMutableArray *checkedObjects = [[[NSMutableArray alloc] init] autorelease];
for (int i=0; i<tableDataSource.count; i++)
{
if ([tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:i inSection:0]].accessoryType == UITableViewCellAccessoryCheckmark)
{
[checkedObjects addObject:[tableDataSource objectAtIndex:i]];
}
}
return checkedObjects;
}
これにより、オンデマンドでデータを取得できます。Jasarienの方法を使用するよりもはるかに効率が悪いことに注意してください。ただし、より良い解決策である場合もあります。
于 2011-09-01T00:32:53.723 に答える