0
我正在使用tableView,需要通過拖放來啓用交換項目。使用Swift 2.0拖放交換
我主要是在那裏,但卡住了一個問題。拖動到的項目未與從中拖出的項目交換。相反,將新的拖動項目添加到現有項目的頂部,它只是將現有項目向下移動一行。
case UIGestureRecognizerState.Ended:
var center = My.cellSnapshot!.center
center.y = locationInView.y
My.cellSnapshot!.center = center
if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
//check to see if this is a valid place to swap
var numberOfSlots:Int = 0 //the number of valid positions to swap
numberOfSlots = thePlayers[Path.initialIndexPath!.row].SwapPosition.count
for index in 1...numberOfSlots {
if thePlayers[Path.initialIndexPath!.row].SwapPosition[index - 1] == thePlayers[indexPath!.row].PlayerPosition {
//this is a valid swap
print("Swap it!")
OkToSwap = true
break
}
else {
print (thePlayers[Path.initialIndexPath!.row].SwapPosition[index - 1])
}
}
}
if OkToSwap == true { //if this is a valid slot for a swap, then swap it.
// swap(&itemsArray[indexPath!.row], &itemsArray[Path.initialIndexPath!.row])
//***TODO: Add Webservice call to swap on the website***
//move the selectedRow to the original (dragged from) position
tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
My.cellSnapshot?.alpha = 0.0
let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
cell.alpha = 1
}
//put it back
let cell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
cell.hidden = false
cell.alpha = 0.0
UIView.animateWithDuration(0.25, animations: {() -> Void in
My.cellSnapshot!.center = originalCenter
My.cellSnapshot!.transform = CGAffineTransformIdentity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0
}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil
}
})
default:
let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
cell.hidden = false
cell.alpha = 0.0
// Clean up.
UIView.animateWithDuration(0.25, animations: {() -> Void in
My.cellSnapshot!.center = cell.center
My.cellSnapshot!.transform = CGAffineTransformIdentity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0
}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil
}
})
}
}
任何想法如何做到使用雨燕2.0行的真正的交換 - 我發現了一些簡單的代碼片段,但語法變化使他們無法工作,我被困。