1

我有一个 UITableViewController,它显示 NSMutableArray 中的项目列表,以及一个用作用户可以选择/取消选择的复选框的按钮。我能够成功显示表格以及复选框。但是,我想在表格的最顶部有一个表头,它有一个“全选”复选框,允许用户选择所有单元格,或取消选择所有单元格。如果某些单元格已经被选中,那么我只想选择那些未选中的单元格。

这是我到目前为止的代码:

- (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

取另一个NSMUtableArray作为 SelectedArray

在行中,didselectRowAtIndexPath您可以从 中添加删除对象SelectedArray

您可以选择一个单元格调用表格视图的selectRowAtIndexPath方法:

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

for loop仅在选定的单元格中放入selectedArrayputcheckbutton。

在你的 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

如果你想在 UITableView 标题中使用以下方法:

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

从此方法返回 UIView 对象,其中包含复选框和您想要的其他内容。

要选中取消选中,请维护一个数组,您可以在其中存储复选框是否被选中。

当用户选中/取消选中标题更改数组并重新加载表时。

于 2013-01-08T06:35:16.837 回答
0

在这里我发布另一个答案,以便它可能有助于其他想要从头开始制作并消除任何困惑的人。

tableViewCell您可以使用 ImageView 作为 AccessoryView,而不是添加 Button 。

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改变imageViewinselectRowAtindexPath

从我的第一个答案中,您可以使用代码选择 tableView 的全部或多个单元格。

于 2013-01-08T07:50:28.050 回答