[英]iOS UICollectionView cell selection per section
I was wondering how to set up my UICollectionView
so that up to 1 cell can be selected per section. 我想知道如何设置我的UICollectionView
以便每个部分最多可以选择1个单元格。 I see that there is the allowsMultipleSelection
property for UICollectionView but I'm wondering how to prevent multiple cells from being selected in the same section. 我看到allowsMultipleSelection
有allowsMultipleSelection
属性,但我想知道如何防止在同一节中选择多个单元格。
Do I need to implement logic in the – collectionView:shouldSelectItemAtIndexPath:
and collectionView:shouldDeselectItemAtIndexPath:
methods or is there a simpler way? 我是否需要在– collectionView:shouldSelectItemAtIndexPath:
和collectionView:shouldDeselectItemAtIndexPath:
方法中实现逻辑,还是有更简单的方法?
Thanks! 谢谢!
In your didSelectRowAtIndexPath you could do something like this: 在你的didSelectRowAtIndexPath中,你可以这样做:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSArray * selectedRows = self.tableView.indexPathsForSelectedRows;
for (NSIndexPath * selectedRow in selectedRows) {
if ((selectedRow.section == indexPath.section) && (selectedRow.row != indexPath.row)) {
[self.tableView deselectRowAtIndexPath:selectedRow animated:NO];
}
}
}
allowsMultipleSelection should be set to YES. allowsMultipleSelection应设置为YES。
Hope it helps! 希望能帮助到你!
In Swift 2.0 : 在Swift 2.0中:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.allowsMultipleSelection = true
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let selectedRows: [NSIndexPath] = self.collectionView.indexPathsForSelectedItems()!
for selectedRow: NSIndexPath in selectedRows {
if (selectedRow.section == indexPath.section) && (selectedRow.row != indexPath.row) {
self.collectionView.deselectItemAtIndexPath(selectedRow, animated: false)
}
}
}
You'll probably need to do logic in -shouldSelect
and -shouldDeselect
. 您可能需要在-shouldSelect
和-shouldDeselect
执行逻辑。 Maybe keep a dictionary of number of cells selected per section ? 也许保留每个部分选择的单元格数字典 ?
NSMutableDictionary *dict;
- (BOOL)collectionView:(UICollectionView *)view shouldSelectItemAtIndexPath:(NSIndexPath *)path
{
NSInteger *section = path.section;
if (!dict)
{
dict = [[NSMutableDictionary alloc] init];
}
NSNumber *numberSelected = [dict objectForKey:[NSNumber numberWithInteger:section]];
if (numberSelected > 0)
{
return NO;
}
else
{
[dict setObject:[NSNumber numberWithDouble:numberSelected.doubleValue - 1] forKey:[NSNumber numberWithInteger:section]];
return YES;
}
}
- (BOOL)collectionView:(UICollectionView *)view shouldDeselectItemAtIndexPath:(NSIndexPath *)path
{
NSInteger *section = path.section;
if (!dict)
{
dict = [[NSMutableDictionary alloc] init];
}
NSNumber *numberSelected = [dict objectForKey:[NSNumber numberWithInteger:section]];
numberSelected = [NSNumber numberWithDouble:numberSelected.doubleValue - 1];
[dict setObject:numberSelected forKey:[NSNumber numberWithInteger:section]];
return YES;
}
That should work. 这应该工作。
In Swift 5.0: 在Swift 5.0中:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.allowsMultipleSelection = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
(collectionView.indexPathsForSelectedItems ?? [])
.filter { $0.section == indexPath.section && $0.item != indexPath.item && $0.row != indexPath.row }
.forEach { self.collectionView.deselectItem(at: $0, animated: false) }
}
if you want it to toggle between items in section use this: 如果你想让它在部分中的项之间切换使用这个:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.allowsMultipleSelection = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
(collectionView.indexPathsForSelectedItems ?? [])
.filter { $0.section == indexPath.section && $0.item }
.forEach { self.collectionView.deselectItem(at: $0, animated: false) }
}
I think that makes better user experience but it's your call 我认为这会带来更好的用户体验,但这是您的号召
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.