我最近开始处理一个小iOS
项目,我有一个由UITableView
代表的游戏分数排行榜,每当玩家获得新的高分时,排行榜可见并且他的分数输入(包括图片)由UITableViewCell
表示的,名称和分数应该移动到它现在所属的TableView
排行榜中的新右侧Spot。新index
的计算工作正常,但cell
根本没有移动。
一些信息:
排行榜成功填充,
我将func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
设置为true
还实施了func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
正确。
我在想,也许我错过了一些东西,但问题也可能在其他地方,我不知道,真的很感激一些帮助。
委托Mehods进行编辑
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])
}
答案 0 :(得分:2)
此代码有效,但它只是示例。适应你。创建新文件并将此代码放入其中,构建并检查。
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()并输入当前的indexPath和indexPath来插入你需要的东西。
答案 1 :(得分:0)
尝试使用此代码移动行向上/向下
var longPress = UILongPressGestureRecognizer (target: self, action:#selector(self.longPressGestureRecognized(_:)))
ToDoTableView.addGestureRecognizer(longPress)
// MARK:-longPressGestureRecognized
func longPressGestureRecognized(_ gestureRecognizer: UIGestureRecognizer) {
let longPress = gestureRecognizer as! UILongPressGestureRecognizer
let state = longPress.state
let locationInView = longPress.location(in: ToDoTableView)
let indexPath = ToDoTableView.indexPathForRow(at: locationInView)
struct Path {
static var initialIndexPath : IndexPath? = nil
}
switch state {
case UIGestureRecognizerState.began:
if indexPath != nil {
let cell = ToDoTableView.cellForRow(at: indexPath!) as UITableViewCell!
UIView.animate(withDuration: 0.25, animations: { () -> Void in
cell?.alpha = 0.0
}, completion: { (finished) -> Void in
if finished {
UIView.animate(withDuration: 0.25, animations: { () -> Void in
cell?.alpha = 1
})
} else {
cell?.isHidden = true
}
})
}
case UIGestureRecognizerState.changed:
if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
itemsArray.insert(itemsArray.remove(at: Path.initialIndexPath!.row), at: indexPath!.row)
ToDoTableView.moveRow(at: Path.initialIndexPath!, to: indexPath!)
Path.initialIndexPath = indexPath
}
default:
print("default:")
}
}