Я использую swipeAction в своем приложении, и иногда вы можете увидеть ошибку пользовательского интерфейса.Иногда он «прыгает» в конце экрана.Я действительно не знаю, как это описать, поэтому есть видео: https://youtu.be/AG4vGxVD4c8
Это что-то в моем коде?(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
}
Редактировать: добавлено больше методов:
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
}
В StarshipTableViewCell: кажется, что awakeFromNib () никогда не вызывается (я пыталсяположить печать в метод).
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
}
}
Спасибо!