代码之家  ›  专栏  ›  技术社区  ›  Peter

IOS swift将所选单元格置于首位

  •  0
  • Peter  · 技术社区  · 4 年前

    如何度假 tableViewCell 因此,当我单击任何单元格时,它都会移动到单元格的顶部|开头

    示例单元格

    Java
    PHP
    CSS /clicked on/
    HTML
    SQL
    

    预期结果

    CSS
    Java
    PHP
    HTML
    SQL
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Gustavo Conde    4 年前
    class ViewController: UITableViewController {
    
        var languages = [
            "CSS",
            "Java",
            "PHP",
            "HTML",
            "SQL"
        ]
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return languages.count
        }
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
            cell.textLabel?.text = languages[indexPath.row]
            return cell
        }
    
        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            languages.insert(languages.remove(at: indexPath.row), at: 0)
            tableView.beginUpdates()
            tableView.moveRow(at: indexPath, to: [0, 0])
            tableView.endUpdates()
        }
    }
    

    Table reordering on tap

    推荐文章