2

我正在尝试发送电子邮件,例如在 uitableview 中选择所有功能,其中在同一按钮上点击用户可以选中或删除所有选中标记,另外用户还可以选择/取消选择行(在 didSelectRowAtIndexPath 上)。我试着做,但它不能正常工作,这是我的代码。

- (IBAction)selectAll:(id)sender
{
    if(myBoolean)
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
            }
        }

        myBoolean = NO;

        [_selectUnselectButton setTitle:@"Select all Friends" forState:UIControlStateNormal];
    }
    else
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryCheckmark];
                NSLog(@"%d-%d",s,r);
            }
        }

        myBoolean = YES;

        [_selectUnselectButton setTitle:@"Unselect all Friends" forState:UIControlStateNormal];
    }
}


-(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
4

8 回答 8

8

我编写了一个示例代码,我根据您的需要进行了调整。

基本上是

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *unifiedID = @"aCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
    }

    cell.textLabel.text = [self.states objectAtIndex:indexPath.row];

    //if the indexPath was found among the selected ones, set the checkmark on the cell
    cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *state = [self.states objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
        [self.selectedCells removeObject:indexPath];
        [self.selecedStates removeObject:state];
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        [self.selectedCells addObject:indexPath];
        [self.selecedStates addObject:state];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    NSLog(@"%@", self.selecedStates);
}


-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
    return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}


- (IBAction)selectAll:(id)sender {

    [self.selecedStates removeAllObjects];
    [self.selectedCells removeAllObjects];
    NSUInteger numberOfSections = [self.tableView numberOfSections];
    for (NSUInteger s = 0; s < numberOfSections; ++s) {
        NSUInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:s];
        for (NSUInteger r = 0; r < numberOfRowsInSection; ++r) {
            NSIndexPath *idxPath = [NSIndexPath indexPathForRow:r inSection:s];
            [self.selectedCells addObject:idxPath];
            [self.selecedStates addObject:self.states[idxPath.row]];
        }
    }
    [self.tableView reloadData];
}



- (IBAction)deselectAll:(id)sender {
    [self.selecedStates removeAllObjects];
    [self.selectedCells removeAllObjects];
    [self.tableView reloadData];
}


- (IBAction)toggleAll:(id)sender {
    if ([self.states count] == [self.selecedStates count]) {
        [sender setTitle:@"select all"];
        [self deselectAll:sender];
    } else {
        [sender setTitle:@"deselect all"];
        [self selectAll:sender];
   }
}

在行动:

在此处输入图像描述

你在打电话

[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];

对于 tableView 中每个部分的每一行。如果你有很多行,这是非常低效的,因为它会处理不在屏幕上的行。但这不是必需的。只需将每个选定的索引路径放入一个数组并告诉 tableView 重新加载。这将重新加载可见单元格,并且由于-tableView:cellForRowAtIndexPath:单元格的实现,新行将被正确重新配置。

于 2013-01-19T06:42:38.230 回答
5

设置附件视图需要在方法内部tableView:cellForRowAtIndexPath:进行。当你想从外部更换配件时,外部方法需要先更改型号,表明必须在某些单元格中打上复选标记,然后调用reloadDataUITableView

存储检查的单元格的一种方法是对象数组NSIndexSet- 每个部分设置一个索引。在下面的示例中,我展示了单个部分的代码,但您应该了解如何使多个部分工作。

// This variable needs to be declared in a place where your data source can get it
NSMutableIndexSet *selected;

// You need to initialize it in the designated initializer, like this:
selected = [[NSMutableIndexSet alloc] init];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    if ([selected containsIndex:indexPath.row]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    // Do the rest of your code
    return cell;
}

现在,在要设置选中或未选中行的代码中,您只需要调用[selected addIndex:rowToSelect]or [selected removeIndex:rowToUnselect],然后调用表的reloadData.

于 2013-01-19T05:31:03.067 回答
3

用于selectRowAtIndexPath:animated:scrollPosition:选择一行
deselectRowAtIndexPath:animated:取消选择一行。
有关更多信息,请阅读UITableView 文档

于 2013-01-19T05:03:48.490 回答
2

试试这个代码而不是你的旧代码

- (IBAction)selectAll:(id)sender
{
    if(myBoolean)
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
              [self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone];

            }
        }

        myBoolean = NO;

        [_selectUnselectButton setTitle:@"Select all Friends" forState:UIControlStateNormal];
    }
    else
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                 [self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone];
                NSLog(@"%d-%d",s,r);
            }
        }

        myBoolean = YES;

        [_selectUnselectButton setTitle:@"Unselect all Friends" forState:UIControlStateNormal];
    }
}


-(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
于 2013-01-19T05:11:17.700 回答
0

您可以按照这些步骤来实现这一点,

1)你应该有一个mutable数组来存储索引路径

2)您可以做的是,当您点击全部选中或取消全部选中时,执行addremove所有索引路径到/来自数组(您已经),重新加载表以进行更新更改

3)在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath数据源方法中,使用 签入数组if([array containsObject:indexPath]),如果存在则标记它checkedunchecked

4)在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath委托方法中,您应该检查是否已经存在进入数组的索引路径,就像您在第 3 步中所做的那样,并根据条件添加删除 indexpaths,重新加载表以进行更新更改

于 2013-01-19T05:47:46.190 回答
0

取另一个NSMutableArray作为SelectedArray

在行中您可以从SelectedArraydidSelectRowAtIndexPath添加删除对象。

您可以选择UITableViewCell调用UITableViews selectRowAtIndexPath方式:

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

为selectedArray放置 for 循环以仅在选定单元格或所有单元格中放置复选标记。

在此处查看我接受的答案:需要为 UITableView 创建一个全选按钮并将选择添加到 iOS 中的数组

于 2013-01-19T06:22:27.180 回答
0

  • Xcode 8x
  • 斯威夫特 3x

    选择行

    let indexPath = IndexPath(row: 0, section: 0)
    mytableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
    myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath)
    

    取消选择行

    let deselectIndexPath = IndexPath(row: 7, section: 0)
    myTableView.deselectRow(at: deselectIndexPath, animated: true)
    myTableView.delegate?.tableView!(tblView, didDeselectRowAt: indexPath)
    
  • 于 2017-05-03T09:26:10.903 回答
    -1

    你可以这样做:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self doSomethingWithRowAtIndexPath:indexPath];
    }
    
    于 2013-01-19T05:07:33.990 回答