我在我的应用程序中使用 swipeAction,有时您会看到 UI 错误。有时它会在屏幕末尾“跳跃”。实在不知道怎么形容,所以有视频:https ://youtu.be/AG4vGxVD4c8
它在我的代码中吗?(斯威夫特 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() (我试图将 print 放入该方法中)。
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
}
}
谢谢!