每节iOS UICollectionView单元格选择

时间:2013-03-12 21:53:08

标签: iphone ios uicollectionview uicollectionviewcell

我想知道如何设置我的UICollectionView,以便每个部分最多可以选择1个单元格。我看到UICollectionView有allowsMultipleSelection属性,但我想知道如何防止在同一部分中选择多个单元格。

我是否需要在– collectionView:shouldSelectItemAtIndexPath:collectionView:shouldDeselectItemAtIndexPath:方法中实现逻辑,还是有更简单的方法?

谢谢!

4 个答案:

答案 0 :(得分:9)

在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应设置为YES。

希望它有所帮助!

答案 1 :(得分:0)

您可能需要在-shouldSelect-shouldDeselect中执行逻辑操作。也许保留每个部分选择的单元格数量字典

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;
}

这应该有效。

答案 2 :(得分:0)

在Swift 2.0中:

import re

if re.search("world \d", line):
   ...

答案 3 :(得分: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) }
}

如果您希望它在本节的各个项目之间切换,请使用以下方法:

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) }
}

我认为这可以带来更好的用户体验,但这是您的电话