Reputation: 79
I want to make the button hidden just if its title equals the 'str' variable.
I can not add more photos but it's doing the same for "7:00", "12:00", "17:00", "22:00" values. Just want for "2:00" and doing comparison for that.
Is this a (collectionView)cellForItemAtIndexPath bug or anything else? I'm so confused. Please help..
Upvotes: 1
Views: 1001
Reputation: 849
When you scrolling it will re use the cell so you set hidden true false based on condition. i think this is your issue.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let hour = array[indexPath.row]
let str ="2:00"
if(cell.hourBtn.titleLabel?.text ==str {
cell.hourBtn.hidden = true
}
else {
cell.hourBtn.hidden = false
}
}
Upvotes: 1
Reputation: 72420
There is no bug in cellForItemAtIndexPath
, its you that hiding the label
of cell. So it just hide the label not remove the cell. If you want to display only that value that is not equal to 2:00
then you need to create one extra array for that and give it to the CollectionViewDelegate
methods. Create one global array and function like this.
var array: [Int] = [Int]()
fun populateData() {
for i in oldArray {
if i != 2 {
self.array.append(i)
}
}
collectionView.reloadData()
}
Now use this array
object in CollectionViewDelegate
methods like this
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.array.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let hour = array[indexPath.row]
cell.hourBtn.setTitle("\(hour):00", forState: .Normal)
//Now there is no need to write code for hiding button
}
Hope this will help you.
Upvotes: 1