I am using swipeAction in my app and sometimes you can see UI bug. Sometimes it "jumps" at the end of the screen. I don't really know how to describe it, so there is video: https://youtu.be/AG4vGxVD4c8
Is it something in my code? (Swift 4.2)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let favouriteAction = self.favouriteAction(forRowAtIndexPath: indexPath)
let swipeConfig = UISwipeActionsConfiguration(actions: [favouriteAction])
return swipeConfig
}
func favouriteAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
let starship = allStarships[indexPath.row].name
let isFav = LibraryAPI.shared.isFavourite(starshipName: starship)
let action = UIContextualAction(style: .normal, title: nil) { (action, view, completion) in
if isFav {
LibraryAPI.shared.removeStarshipFromFavourites(starshipName: starship)
self.allStarships[indexPath.row].isFavourte = false
self.tableView.reloadRows(at: [indexPath], with: .none)
} else {
LibraryAPI.shared.addStarshipToFavourites(starshipName: starship)
self.allStarships[indexPath.row].isFavourte = true
self.tableView.reloadRows(at: [indexPath], with: .none)
}
completion(true)
}
action.backgroundColor = UIColor.starWarsColor.darkBlue
action.image = isFav ? UIImage(named: "favourite") : UIImage(named: "unfavourite")
return action
}
Edit: added more methods:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let starshipsNames = LibraryAPI.shared.getUsersStarshipsNames()
let cell = self.tableView.dequeueReusableCell(withIdentifier: "starshipCell", for: indexPath) as! StarshipTableViewCell
cell.name.text = allStarships[indexPath.row].name
cell.model.text = allStarships[indexPath.row].model
cell.manufacturer.text = allStarships[indexPath.row].manufacturer
if let cellName = cell.name.text {
if starshipsNames.contains(cellName) {
cell.setAsUsers(isUsers: true)
} else {
cell.setAsUsers(isUsers: false)
}
}
cell.setFavourite(isFavourite: allStarships[indexPath.row].isFavourte ?? false)
if indexPath.row == allStarships.count - 1 { // last cell
loadMoreItems()
}
return cell
}
In StarshipTableViewCell: It seems that awakeFromNib() is never called (I tried to put print into the method).
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(favouriteImage)
self.addSubview(starshipImage)
self.addSubview(name)
self.addSubview(model)
self.addSubview(manufacturer)
layoutSetup()
}
func setAsUsers(isUsers: Bool) {
if isUsers {
starshipImage.image = UIImage(named: "sw-starship")!
starshipImage.backgroundColor = UIColor.starWarsColor.darkBlue
starshipImage.tintColor = UIColor.starWarsColor.yellow
} else {
starshipImage.image = UIImage(named: "sw-starship-angle")!
starshipImage.backgroundColor = .lightGray
starshipImage.tintColor = UIColor.starWarsColor.darkBlue
}
}
func setFavourite(isFavourite: Bool) {
if isFavourite {
favouriteImage.backgroundColor = UIColor.starWarsColor.darkBlue
} else {
favouriteImage.backgroundColor = .clear
}
}
Thanks!