iOS UITableView 在代码中移动单元格

标签 ios swift xcode uitableview

我最近开始开发一个小型 iOS 项目,其中有一个由 UITableView 表示的游戏分数排行榜,每当玩家获得新的高分时,排行榜就会可见,并且他的由 UITableViewCell 表示的分数条目(包括图片、姓名和分数)应该移动到它现在所属的 TableView 排行榜中新的右侧位置。新索引的计算工作正常,但cell根本没有移动。

一些信息:

  • 排行榜已成功填充,

  • 我将 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool 设置为 true

  • 还实现了 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)

正确。

我想也许我错过了一些东西,但问题也可能出在其他地方,我不知道,并且非常感谢一些帮助。

委托(delegate)编辑方法

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}
// Handles reordering of Cells
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let player = players[sourceIndexPath.row]
    players.remove(at: sourceIndexPath.row)
    players.insert(player, at: destinationIndexPath.row)
}

我尝试移动行的代码

func newHighscore(highscore: Int) {
    friendsTableView.reloadData()
    let myNewIndex = Player.recalculateMyIndex(players: players, score: highscore)
    friendsTableView.moveRow(at: [0,Player.myIndex], to: [0,myNewIndex])

}

最佳答案

这段代码可以工作,但它只是示例。为您调整它。创建新文件并将此代码放入其中,构建并检查。

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!


    var scores = ["1", "2", "3", "4", "5"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func move(from: IndexPath, to: IndexPath) {
        UIView.animate(withDuration: 1, animations: {
            self.tableView.moveRow(at: from, to: to)
        }) { (true) in
            // write here code to remove score from array at position "at" and insert at position "to" and after reloadData()
        }
    }

    @IBAction func buttonPressed(_ sender: UIButton) {
        let fromIndexPath = IndexPath(row: 4, section: 0)
        let toIndexPath = IndexPath(row: 1, section: 0)
        move(from: fromIndexPath, to: toIndexPath)
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return scores.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? TableViewCell
        if let cell = cell {
            cell.setText(text: scores[indexPath.row])
        }
        return cell ?? UITableViewCell()
    }

    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }

}   

为了正确使用,您需要最后在数组中插入新元素,重新加载数据,然后您可以调用方法 move() 并将当前的索引路径和索引路径放入其中以插入您需要的元素。

关于iOS UITableView 在代码中移动单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49554820/

相关文章:

ios - 如何在容器 View 中的 tableview 周围获取可见框架

ios - Swift - 具有自引用的变量函数

ios - 当我展示我的 UIImagePickerController 时,什么也没有出现

ios - 创建一个 SKShapeNodes 数组

ios - 在使用 Xcode 10 生成 dSYM 后运行脚本阶段(构建时)

ios - Xcode 调试/仪器 : See all pointers to an object

iphone - 另一个 "message sent to deallocated instance"

ios - 快速调整父 UIView 的自动布局约束,其标签数量为 ' N ',作为 subview

ios - 将共享文档文件夹中的文档加载到WKWebView中

javascript - 处理 URL 方案 react-native 和 Swift