对于iPhone,是否可以配置UITableView以允许多选?

我已经尝试覆盖每个UITableViewCell的-setSelected:animated:,但是试图捏造所需的行为是很棘手的,因为很难将真正的取消选择与UITableView认为由于选择另一个单元格而未选择的那些取消分离!

希望有人可以提供帮助!

谢谢,

尼克。

有帮助吗?

解决方案

执行此操作的最佳方法是选中每行的复选标记。

您可以通过将所选UITableViewCell实例上的accessoryType设置为UITableViewCelAccessoryCheckmark来实现。

要取消选择该行,请将其重新设置为UITableViewCellAccessoryNone。

要枚举选择了哪些单元格/行(例如,单击按钮时),只需遍历表格的单元格以查找UITableViewCellAccessoryCheckmark。或者,在<!>中的表视图委托中管理一些NSSet等;确实选择<!>“;委托方法。

其他提示

如果您正在为iOS5.0 +

开发应用程序,以下属性应该可以正常工作
self.tableView.allowsMultipleSelection = YES;

使用以下代码设置单元格附件类型:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else{
        thisCell.accessoryType = UITableViewCellAccessoryNone;

    }
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

//add your own code to set the cell accesory type.
return UITableViewCellAccessoryNone;
}

Jeff Lamarche在这里有一个关于如何做到这一点的教程:

http://iphonedevelopment.blogspot。 COM / 2008/10 /表视图的多行编辑mode.html

我没有尝试过这段代码,但是我已经想到了一段时间,知道我需要它的那一天会来。

我将allowsMultipleSelectionDuringEditingallowsMultipleSelection从iOS5反向移植到旧iOS。您可以在 https://github.com/ud7/UDTableView-allowsMultipleSelection

它是替代品,你只需要做的就是将UITableView更改为UDTableView(在代码或界面构建器中)

来自HIG:

  

表视图在用户选择列表项时提供反馈。具体而言,当可以选择项目时,   当用户选择该项以显示已收到选择时,包含该项的行会略微突出显示。   然后,立即执行操作:显示新视图或行显示复选标记以指示   该项目已被选中。该行永远不会突出显示,因为表视图不显示   持久选择状态。

您需要使用类似Mail的内容或使用单元格上的复选标记附件来滚动自己的多种选择样式。

您需要多重选择的人

self.tableView.allowsMultipleSelection = YES;

在viewDidLoad和

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryView.hidden = NO; 
    // if you don't use custom image tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryView.hidden = YES;
    // if you don't use custom image tableViewCell.accessoryType = UITableViewCellAccessoryNone;
}

如果您尝试执行类似Mail的多选(例如删除邮件),那么您可能需要自己管理所有选择。多行选择不是iPhone的标准选择。 Mail通过使用复选标记来指示选择了哪些行。

根据蓝色高亮显示的行作为指示是否选择了行的实际操作。 pdf“rel =”nofollow“> HIG 第121页。检查标记可以解决问题。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    int selectedRow = indexPath.row;
    cout << "selected Row: " << selectedRow << endl;
    UITableViewCell *indexPathForCell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPathForCell.accessoryType == UITableViewCellAccessoryNone) {
        indexPathForCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        indexPathForCell.accessoryType = UITableViewCellAccessoryNone;
    }

}

然后添加您的排列或您希望如何存储选择的数据。

我正在寻找同样的问题,而Bhavin Chitroda的答案为我解决了这个问题,但是在滚动的同时保留了复选标记。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if ( [array indexOfObject:indexPath] == NSNotFound ) {
            [array addObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            [array removeObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

}

补充:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// Your code here
.
.
.
    if ( [array indexOfObject:indexPath] == NSNotFound ) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    return cell;

}

注意:这在iOS 4+中不起作用。这是一个私有的,未记录的常量。不要使用它。

如果您不打算将应用程序提交到App Store,可以通过在UITableViewController委托中实现以下方法来调用多行编辑模式:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 3; // Undocumented constant
}

使用iOS4.3 - 6.0进行测试

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {

    if ([controller.searchResultsTableView respondsToSelector:@selector(allowsMultipleSelectionDuringEditing)]) {
        controller.searchResultsTableView.allowsMultipleSelectionDuringEditing = YES;
    }
    else {
        controller.searchResultsTableView.allowsSelectionDuringEditing = YES;
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellAccessoryCheckmark;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top