iPhone-是否可以将UITableView配置为允许多选?
对于iPhone,是否可以配置UITableView使其允许多选?
我已经尝试为每个UITableViewCell覆盖-setSelected:animated:
,但是尝试捏合所需的行为非常棘手,因为很难将真正的未选择项与UITableView认为由于选择了另一个单元格而未选择的项分开!
希望有人能帮忙!
谢谢,
缺口。
如果您正在为iOS5.0 +开发应用程序,则以下属性应能正常工作
self.tableView.allowsMultipleSelection = YES;
最好的方法是在每个选定的行上打勾。
您可以通过将选定的UITableViewCell实例上的annexType设置为UITableViewCelAccessoryCheckmark来实现。
要取消选择该行,请将其设置回UITableViewCellAccessoryNone。
要枚举选择了哪些单元格/行(例如,单击按钮时),只需遍历表的单元格以查找UITableViewCellAccessoryCheckmark。 或者,在“ did select”委托方法中的表视图委托中管理某些NSSet等。
使用以下代码来设置单元附件类型:
- (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://iPhone development.blogspot.com/2008/10/table-view-multi-row-edit-摸得.HTML]
我没有尝试过代码,但是已经有一段时间了,我知道需要的时候会到来。
我将allowsMultipleSelectionDuringEditing
和allowsMultipleSelection
从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通过使用复选标记指示已选择了哪些行来解决此问题。
根据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;
}