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:-
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:
The number of visible cells you've got, depending on the height of the collection
When scrolling through cells to reuse them
Or triggers to change/refresh the collection like:
reloadData()
,reloadItems(at:)
orreloadSections(_:)
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
How to Use a Specific Gmt for a Function Which Will Be Recognised by Other Time Zones
Contacts Not Recognized When Body Is Changed from Circle to Rectangle
How to Get Exactly the Same Point on Different Screen Sizes
Any Way to Get a Gif as a Background with Swiftui
Compare Value from Two Struct in Swift
Can the Byte Order of Double Be Safely Reversed
How Is a Global Variable Set to Private Understood in Swift
Could Not Cast Value of Type Uiview to [Customview] Using Xib
How to Unpack Multiple Levels of Nested JSON in Firebase Database
Avaudioplayer.Play() Works But Avaudioplayernode.Play() Fails
Swift How to Sort Dict Keys by Byte Value and Not Alphabetically
Setting Observer for Swift Objects/Properties
Collection View Layout Bug When Selectitem (Swift 5)
Swift: Filter a Dictionary with Array as Value
Can't Change Uiimageview Image in Function (Swift)
How to Make Uislider Default Thumb to Be Smaller Like the Ones in the iOS Control Center