3

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

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

谢谢!

4

4 回答 4

10

在您的 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];
        }
    }
}

allowMultipleSelection 应设置为 YES。

希望能帮助到你!

于 2013-03-12T21:57:54.250 回答
2

在 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)
        }
    }
}
于 2016-08-12T13:40:41.887 回答
1

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

我认为这会带来更好的用户体验,但这是你的决定

于 2019-06-29T16:36:28.677 回答
0

您可能需要在-shouldSelectand中执行逻辑-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;
}

那应该行得通。

于 2013-03-12T22:02:11.647 回答