在 CollectionView 中关闭已使用的单元格并打开未使用的单元格
Close used cells and open unused cells in CollectionView
我尝试在 collectionView 中 关闭 使用的单元格并 打开 未使用的单元格。我认为需要在 func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool
我有字符串数组:
var arrayOfStrings: [String] = ["01:00", "05:00", "06:00", "07:00"]
我需要 close 来选择在我的 collectionView 中有来自 arrayOfStrings
的字符串的单元格,然后 open 单元格选择我的阵列中不存在的那个。
我该怎么做?
我是密码:
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
}
还有我的收藏查看图片:
您可以尝试搜索 arrayOfStrings 是否包含要显示的项目,return false 关闭选择
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
let item = allTimeArray[indexPath.row]
for er in arrayOfStrings
if(er == item)
return false
return true
}
或者简而言之
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return !(arrayOfStrings.contains(allTimeArray[indexPath.row]))
}
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])
}
}
我尝试在 collectionView 中 关闭 使用的单元格并 打开 未使用的单元格。我认为需要在 func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool
我有字符串数组:
var arrayOfStrings: [String] = ["01:00", "05:00", "06:00", "07:00"]
我需要 close 来选择在我的 collectionView 中有来自 arrayOfStrings
的字符串的单元格,然后 open 单元格选择我的阵列中不存在的那个。
我该怎么做?
我是密码:
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
}
还有我的收藏查看图片:
您可以尝试搜索 arrayOfStrings 是否包含要显示的项目,return false 关闭选择
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
let item = allTimeArray[indexPath.row]
for er in arrayOfStrings
if(er == item)
return false
return true
}
或者简而言之
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return !(arrayOfStrings.contains(allTimeArray[indexPath.row]))
}
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])
}
}