滑动动作有时会“跳转”到屏幕末端

时间:2019-02-15 21:36:59

标签: swift tableview uiswipeactionsconfiguration

我在我的应用程序中使用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
    }
}

谢谢!

0 个答案:

没有答案