3

UICollectionViewセクションごとに最大1つのセルを選択できるように設定する方法を考えていました。allowsMultipleSelectionUICollectionViewのプロパティがあるようですが、同じセクションで複数のセルが選択されないようにする方法を考えています。

– collectionView:shouldSelectItemAtIndexPath:およびメソッドにロジックを実装する必要がcollectionView: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];
        }
    }
}

permitMultipleSelectionは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

おそらくとでロジックを実行する必要があり-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;
}

それはうまくいくはずです。

于 2013-03-12T22:02:11.647 に答える