如何通过单击选择多个单元格(Objective-c)



我正在编写一个 iOS 应用程序,想知道当用户单击 uitableview 中的单个单元格时是否可以一次选择 3 个单元格。所以 3 将是他们点击的那个和下面的 2。这是否可能,如果是,将如何完成?谢谢

我目前如何检查用户点击:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];

if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
dispatch_async(dispatch_get_main_queue(), ^{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
});
}
}
}

我也有

self.tableView.allowsMultipleSelection = true;

在我的代码中,但问题是它不允许我一次选择 3 个。

要回答您关于单击选择多个单元格的问题(我暂时忽略您的插入行/删除行的东西(......

下面是选择多行的几个示例。根据您的具体情况实现这一点应该没有问题:

目标-C

// select ALL rows in Section 0
for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
NSIndexPath *iPath = [NSIndexPath indexPathForRow:i inSection:0];
[tableView selectRowAtIndexPath:iPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
// select ALL VISIBLE rows
for (NSIndexPath *iPath in [tableView indexPathsForVisibleRows]) {
[tableView selectRowAtIndexPath:iPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
// select rows 2, 4 and 6
for (NSNumber *n in @[@2, @4, @6]) {
NSIndexPath *iPath = [NSIndexPath indexPathForRow:[n integerValue] inSection:0];
[tableView selectRowAtIndexPath:iPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
// deselect all rows
for (NSIndexPath *iPath in [tableView indexPathsForSelectedRows]) {
[tableView deselectRowAtIndexPath:iPath animated:NO];
}

迅速

// select ALL rows in Section 0
for i in 0..<tableView.numberOfRows(inSection: 0) {
let iPath = IndexPath(item: i, section: 0)
tableView.selectRow(at: iPath, animated: false, scrollPosition: .none)
}
// select ALL VISIBLE rows
for iPath in tableView.indexPathsForVisibleRows! {
tableView.selectRow(at: iPath, animated: false, scrollPosition: .none)
}
// select rows 2, 4 and 6
for i in [2, 4, 6] {
let iPath = IndexPath(item: i, section: 0)
tableView.selectRow(at: iPath, animated: false, scrollPosition: .none)
}
// deselect all rows
for iPath in tableView.indexPathsForSelectedRows! {
tableView.deselectRow(at: iPath, animated: false)
}

最新更新