1

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];

        }
    }
}

誰かがこれを行う方法について何か提案がありますか?

返信してくださった皆様、ありがとうございました。

4

3 に答える 3

0

NSMUtableArraySelectedArrayとして別のものを取る

didselectRowAtIndexPath行内からオブジェクトを追加削除できますSelectedArray

selectRowAtIndexPathテーブルビューのメソッドを呼び出すセルを選択できます。

[yourtable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

選択したセルにのみputcheckbuttonを配置しますfor loopselectedArray

CellForRowメソッドでこれを使用します

testButton.tag=indexpath.row;

以下の方法ですべての行とすべてのセクションを選択できます。

for (int i = 0; i < [ptableView numberOfSections]; i++) {
    for (int j = 0; j < [ptableView numberOfRowsInSection:i]; j++) {
        NSUInteger ints[2] = {i,j};
        NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [ptableView cellForRowAtIndexPath:indexPath];
           //Here is your code

      UiButton *btn = (UiButton*)[cell.contentView viewWithTag:j]

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
    }
        }
    }
于 2013-01-08T06:26:33.047 に答える
0

これをスクラッチから作成して混乱を取り除きたい他の人に役立つように、別の回答を投稿しています。

Button を に追加する代わりにtableViewCell、ImageView を AccessoriesView として使用できます。

UIImageView *imageView1 = [[[UIImageView alloc] init] autorelease];
imageView1.tag = indexpath.row;

[cell.accessoryView addSubview:imageView1];

viewWithTagその後、 - : メソッドを使用してサブビューを取得します。

UIImageView *getImageView1 = (UIImageView*)[cell.accessoryView viewWithTag:rowNumber];
getImageView1.image=[UIImage ImageNamed:@"checked.png"];

上記のコードにより、のを変更imageできimageViewますselectRowAtindexPath

私の最初の回答から、コードを使用してtableViewのすべてまたは複数のセルを選択できます。

于 2013-01-08T07:50:28.050 に答える
0

UITableView ヘッダーでそれが必要な場合は、次の方法を使用します。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

このメソッドから、チェックボックスとその他必要なものを含む UIView オブジェクトを返します。

チェックを外すには、チェックボックスがチェックされているかどうかを格納できる配列を維持します。

ユーザーがヘッダーをチェック/チェック解除すると、配列が変更され、テーブルがリロードされます。

于 2013-01-08T06:35:16.837 に答える