IOS swift 将所选单元格置顶
IOS swift bring selected cell to top
如何求助 tableViewCell
所以当我点击任何单元格时它会移动到单元格的顶部|开头
示例单元格
Java
PHP
CSS /clicked on/
HTML
SQL
预期结果
CSS
Java
PHP
HTML
SQL
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()
}
}
如何求助 tableViewCell
所以当我点击任何单元格时它会移动到单元格的顶部|开头
示例单元格
Java
PHP
CSS /clicked on/
HTML
SQL
预期结果
CSS
Java
PHP
HTML
SQL
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()
}
}