关闭用过的单元格并在CollectionView中打开未使用的单元格

时间:2018-02-18 10:27:48

标签: ios swift xcode collections uicollectionviewcell

我尝试在 collectionView 关闭使用过的单元格和打开未使用的单元格。我认为需要在func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool

中制作

我有一系列字符串:

var arrayOfStrings: [String] = ["01:00", "05:00", "06:00", "07:00"]

我需要关闭以选择我的collectionView中包含arrayOfStrings字符串的单元格,并选择打开单元格以供我选择阵列。

我怎么做?

这是我的代码:

var allTimeArray: [String] = ["01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00"]
var arrayOfStrings: [String] = ["01:00", "05:00", "06:00", "07:00"]

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return allTimeArray.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookTimeCell

    cell.timeLabel.text = allTimeArray[indexPath.item]

    return cell

}

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {

    // i think need use IF...
    return true

}

我的collectionView图片:

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以尝试搜索arrayOfStrings是否包含要显示的项目,并返回false以关闭选择

public class Consumer {

    public void buy(Object o) {
        System.out.println("Buying one object");
    }

    public void buy(Object... o) {
        System.out.println("Buying multiple objects");
    }

    public static void main(String[] args) {
        Consumer consumer = new Consumer();
        consumer.buy(new Object());
        consumer.buy("a String");
    }

}

或简而言之

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {

 let item = allTimeArray[indexPath.row]
    for er in arrayOfStrings
     if(er == item)
       return false

  return true

}

答案 1 :(得分:0)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookTimeCell

    cell.timeLabel.text = allTimeArray[indexPath.item]

    if arrayOfStrings.contains(cell.timeLAbel.text) {
        //configure for choosed cell.
    } 
    else {
        //Configure for open cell.
    }

    return cell

}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    let selectedString = allTimeArray[indexPath.item]
    if !arrayOfStrings.contains(selectedString)
    {
        arrayOfStrings.append(selectedString)

        //Reload the cell at indexpath.
        collectionView.reloadItems(at: [indexPath])
    }
}