NSMutableArrayのアイテムのリストを表示するUITableViewControllerと、ユーザーが選択/選択解除できるチェックボックスとして機能するボタンがあります。テーブルとチェックボックスを正常に表示できます。ただし、テーブルの最上部に「すべて選択」チェックボックスが付いたテーブルヘッダーが必要です。これにより、ユーザーはすべてのセルを選択したり、すべてのセルの選択を解除したりできます。一部のセルがすでにオフになっている場合は、選択されていないセルのみを選択する必要があります。
これが私がこれまでに持っているコードです:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(280, 57, 25, 25)];
[testButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected];
[testButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
[testButton setUserInteractionEnabled:YES];
[testButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[cell setAccessoryView:testButton];
}
// Configure the cell...
TestObject *tObject = [[DataModel sharedInstance].testList objectAtIndex:indexPath.row];
cell.textLabel.text = tObject.testTitle;
return cell;
}
-(void)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"CheckBox1.png"]])
{
[btn setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
// other statements
}
else
{
[btn setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateNormal];
// other statements
}
}
//Here is the additional method that I have added to my code to select all
-(void)clickOnCheckButton {
NSLog(@"Did it select?");
for (int i = 0; i < [self.tableView numberOfSections]; i++) {
for (int j = 0; j < [self.tableView numberOfRowsInSection:i]; j++) {
NSUInteger ints[2] = {i,j};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Here is your code
[self buttonTouched:nil];
}
}
}
誰かがこれを行う方法について何か提案がありますか?
返信してくださった皆様、ありがとうございました。