Uicollectionviewcell Calling Functions Over and Over

Call function for one UICollectionViewCell only

Add this line after cell initialization,

cell.removeConstraints(cell.constraints)

So will look like this,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
cell.removeConstraints(cell.constraints)

if let CurrentPost = posts[indexPath.row] as? Post{
//determine which constraint to call
if(CurrentPost.PostText != nil){
if(CurrentPost.PostImage != nil){
cell.postImage.image = CurrentPost.PostImage
cell.cellConstraintsWithImageWithText()
}else{
cell.postImage.image = nil
cell.cellConstraintsWithoutImageWithText()
}
}else{
cell.postImage.image = CurrentPost.PostImage
cell.cellConstraintsWithImageWithoutText()
}
}
return cell
}

Call function from UICollectionViewCell to use in UICollectionView

The flow is inversed but yes, the easiest way is via notifications
in you collection view

Put this in your collectionView button pressed method

NotificationCenter.default.post(name: Notification.Name("handleNewJob"), object: nil)

Add observer in you collection view cell (init or awakeFromNib depends)

NotificationCenter.default.addObserver(self, selector: #selector(handleNewJob, name: NSNotification.Name(rawValue: "handleNewJob"), object: nil)

How to create/call functions inside cellForItemAt in a UICollectionView

Hey, Xcode is very intelligent, This type of task Xcode do automatically. Just follow the few steps:-

1:- Select the line of code(for which you make a function)

2:- Press right-click, Under that going to the "Refractor" method

3:- Tap on "Extract to Method". It automatically makes the function and by default, it calls from where it should be extracted.

Please see in the image:-

Sample Image

This solution is available for-ever in Xcode.

UICollectionView CellForItemAt being called multiple times on scroll causing cell text to overlap

Because of cell resuing , don't add subviews in cellForRow

dateCell.addSubview(dayLabel)
dateCell.addSubview(weekdayLabel)

//

in cellForRowAt

if indexPath.row == indexOfGray {
// gray color
}
else {
{
// default color
}

design will be either in xib , or custom class of the cell in init method

Why is the CollectionView function called twice in Swift?

collectionView(_:cellForItemAt:) is a collection view delegate method which tends to get called more than once, and there are a lot of factors that trigger the call like:

  1. The number of visible cells you've got, depending on the height of the collection

  2. When scrolling through cells to reuse them

Or triggers to change/refresh the collection like:


  1. reloadData(), reloadItems(at:) or reloadSections(_:)

  2. Perhaps a layoutIfNeeded() call from the view that contains this collection view.

and more...

The point is, the function getting called more than once is pretty normal. Your answer probably lies in "What triggers the call?"

EDIT:

To update your pageControl according to the index of the collection:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let visibleRect = CGRect(origin: yourCollectionView.contentOffset, size: yourCollectionView.bounds.size)
let midPointOfVisibleRect = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
if let visibleIndexPath = yourCollectionView.indexPathForItem(at: midPointOfVisibleRect) {
yourPageControl.currentPage = visibleIndexPath.row
}
}

If you're using a UIPageViewController just for the page indication, a UIPageControl should do. You might also have to set yourCollectionView.isPagingEnabled = true as that would be more appropriate for a page control.



Related Topics



Leave a reply



Submit