I have a TableviewController which has multiple customs cells. One of them has a CollectionView (inside the tableView cell). In the CollectionView Cell I have as Image View where I need to display a map. Everything works but when I navigate to the TableviewController for the first time and scroll down I get a glitch/lag/frame drop at a certain spot. this only happens once and if you go back to the previous view then again to the TableView Controller the lag doesn't seem to be present.
If I don't set the map image then the lag goes away so this is definitely causing that issue.
// TableView cellForRowAt
case .branchesCell:
let cell = tableView.dequeueReusableCell(withIdentifier: "branchesCell", for: indexPath) as! BranchesCell
cell.branches = restaurant?.branches
cell.restaurantName = restaurant?.metadata.name ?? ""
return cell
// Tableview Cell Class - BranchesCell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NestledBranchCell
cell.layer.masksToBounds = false
cell.backgroundColor = .clear
if let branches = branches {
cell.branch = branches[indexPath.row]
cell.restaurantName = restaurantName
}
return cell
}
// Collectionview Cell Class - NestledBranchCell
override func awakeFromNib() {
super.awakeFromNib()
contentView.layer.applySketchShadow(color: .black, alpha: 0.14, x: 0, y: 7, blur: 14, spread: 0)
containerView.clipsToBounds = true
containerView.layer.cornerRadius = 15
tradingStatusLbl.layer.cornerRadius = 10
tradingStatusLbl.layer.masksToBounds = true
directionsBtn.layer.cornerRadius = 10
callBtn.layer.cornerRadius = 10
}
override func draw(_ rect: CGRect) {
super.draw(rect)
setupCell() // Setting a few labels
Task {
await setupMap()
}
}
func setupMap() async {
if let branch = branch {
let mapSnapshotOptions = MKMapSnapshotter.Options()
let location = branch.coordinates.coordinate
let region = MKCoordinateRegion(center: location, latitudinalMeters: 300, longitudinalMeters: 300)
mapSnapshotOptions.region = region
mapSnapshotOptions.scale = UIScreen.main.scale
let size = CGSize(width: 314, height: 195)
mapSnapshotOptions.size = size
mapSnapshotOptions.showsBuildings = true
mapSnapshotOptions.pointOfInterestFilter = .none
let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
snapShotter.start { snapshot, error in
let image = snapshot?.image
self.imageView.image = image
}
}
}
Post
Replies
Boosts
Views
Activity
In my tableview I have a few custom cells. When I scroll in the tableview it lags in the same spot but only for the first time the app is open. if I go back to the root view then open the tableview again it doesn't seem to lag. I've traced the lag to one cell. In that cell I have an embedded collection view and each of those cells has image view with is showing a MKMapSnapshot and also a few labels etc. If run with 0 collectionView cells (numberOfCellsInSection) the tableview scrolls smoothly but lags when I have 1 or more CollectionViewCell cells.
I initially loaded the image in the collectionView cell class in layoutSubviews which worked but I then learned that this is called numerous times so code like that shouldn't be there. if I put it in awakeFromNib the Branch struct doesn't seem to have been passed to the cell as yet (from cellForRowAt) so it doesn't work there. I then moved setting the image in cellForItemAt but I'm still not sure if it is correct there.
// Table View cellForRowAt
case .branchesCell:
let cell = tableView.dequeueReusableCell(withIdentifier: "branchesCell", for: indexPath) as! BranchesCell
cell.branches = restaurant?.branches // This is the info the embedded collectionView cell should display. Text for labels and coordinates for the MKMapSnapshot.
return cell
// TableView Cell Class
class BranchesCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet var collectionView: UICollectionView!
var branches: [Branch]?
var restaurantName = String()
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = .clear
collectionView.delaysContentTouches = false
}
// MARK: UICollectionView
func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 }
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let branches = branches {
return branches.count
} else { return 0 }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NestledBranchCell
cell.layer.masksToBounds = false
cell.backgroundColor = .clear
let imageView = cell.viewWithTag(1) as! UIImageView
if let branches = branches {
cell.branch = branches[indexPath.row]
cell.restaurantName = restaurantName
// Tried to run this in a different thread to see if it would make a difference.
DispatchQueue.main.async() { [self] in
let size = CGSize(width: 314, height: 195)
// Set the image here
getMapImage(branch: branches[indexPath.row], size: size) { mapImage in
imageView.image = mapImage
}
}
}
return cell
}
func getMapImage(branch: Branch, size: CGSize, completion: @escaping (UIImage?) -> ()) {
var mapImage = UIImage()
let mapSnapshotOptions = MKMapSnapshotter.Options()
let location = branch.coordinates.coordinate
let region = MKCoordinateRegion(center: location, latitudinalMeters: 300, longitudinalMeters: 300)
mapSnapshotOptions.region = region
mapSnapshotOptions.scale = UIScreen.main.scale
mapSnapshotOptions.size = size
mapSnapshotOptions.showsBuildings = true
mapSnapshotOptions.pointOfInterestFilter = .none
let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
snapShotter.start { snapshot, error in
let image = snapshot?.image
mapImage = image!
completion(mapImage)
}
}
}
One part of my app uses CreateML but I still need to test the UI on other screen sizes with the simulator. I tried using this condition but still get the same error.
#if os(iOS)
import CreateML // CreateML is not available when building for iOS Simulator. Consider using `#if os(iOS)` to conditionally import this framework when building for iOS.
#endif
I have button inside a CollectionViewCell and when tapped I want to run an animation. The animation doesn't run and I'm not sure why.
Inside the Cell class
@IBAction func hoursExpandBtnTapped(_ sender: Any) {
UIView.animate(withDuration: 3, delay: 0, options: .curveLinear) { [self] in
addressLbl.frame = CGRect(x: addressLbl.frame.minX, y: addressLbl.frame.minY, width: 20, height: addressLbl.frame.height)
} completion: { _ in
}
}
I'm trying to access another class in my CollectionView Controller but when I try and initialise the class I get the error
Here is the class
@MainActor
class AlbumsViewModel {
private let albumService: AlbumService
private let favouritesService: FavouritesService
private let recommendationService: RecommendationService
@Published var favourites = [Bool]()
@Published var recommendedAlbums = [Album]()
@Published var allAlbums = [Album]() {
didSet {
updateFavourites()
}
}
init(albumService: AlbumService, favouritesService: FavouritesService, recommendationService: RecommendationService) {
self.albumService = albumService
self.favouritesService = favouritesService
self.recommendationService = recommendationService
}
...(functions here)
Collectionview Controller Class
class CVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var viewModel: AlbumsViewModel
init?(viewModel: AlbumsViewModel, coder: NSCoder) {
self.viewModel = viewModel
super.init(coder: coder)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delaysContentTouches = false
Task {
print(viewModel.allAlbums)
}
} // End viewDidLoad
...
Trying to recreate this https://martinmitrevski.com/2021/07/11/ml-recommendation-app-with-create-ml-on-ios-15/
In a section of my app I would like to recommend restaurants to users based on certain parameters. Some parameters have a higher weighting than others. In this WWDC Video a very similar app is made.
Here if a user likes a dish a value of 1.0 is assigned to those specific keywords (that apple to the dish) with a value of -1.0 to all other keywords that don't apply to that dish.
For my app if the user has ordered I then apply a value of 1.0 (to the keywords that apply) but if a user has just expressed interest (but not ordered) then can I apply a value of 0.5 (and -0.5) instead, would the model adapt to this?
In my Table View I have the label text set to a different colour if the name is in the Flagged Array (flaggedNames). The correct cell labels turn red but as I scroll the text of other labels are also red. This changes as I scroll.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = names[indexPath.row]
cell.textLabel?.text = name
if flaggedNames.contains(meterNumber) {
cell.textLabel?.textColor = .systemRed
}
return cell
}