IOS UITableViewCell 移動(dòng)單元格位置

1 import UIKit
2
3 class ViewController:UIViewController,
UITableViewDataSource, UITableViewDelegate{
4
5 var customers = [“[普通客戶]冮炳林”, “[普通客戶]扶伽
霖”, “[普通客戶]岡皚冰”,
6 “[金牌客戶]符博富”, “[普通客戶]范姜臣華”]
7
8 override func viewDidLoad() {
9 super.viewDidLoad()
10 // Do any additional setup after loading the view,
typically from a nib.
11
12 let screenRect = UIScreen.main.bounds
13 let tableRect = CGRect(x:0, y:20, width:
screenRect.size.width, height:screenRect.size.height - 20)
14 let tableView = UITableView(frame:tableRect)
15
16 tableView.dataSource = self
17 tableView.delegate = self
18 tableView.setEditing(true, animated:true)
19 self.view.addSubview(tableView)
20 }
21
22 func tableView(_ tableView:UITableView,
numberOfRowsInSection section:Int) -> Int{
23 return customers.count
24 }
25
26 func tableView(_ tableView:UITableView,
cellForRowAt indexPath:IndexPath)
27 -> UITableViewCell {
28
29 let identifier = “reusedCell”
30 var cell =
tableView.dequeueReusableCell(withIdentifier:identifier)
31
32 if(cell == nil){
33 cell = UITableViewCell(style:
UITableViewCellStyle.default,
34 reuseIdentifier:identifier)
35 }
36
37 cell?.textLabel?.text = customers[(indexPath as
NSIndexPath).row]
38 return cell!
39 }
40
41 func tableView(_ tableView:UITableView,
editingStyleForRowAt indexPath:IndexPath) ->
UITableViewCellEditingStyle {
42 return UITableViewCellEditingStyle.none
43 }
44
45 func tableView(_ tableView:UITableView,
canMoveRowAt indexPath:IndexPath) -> Bool {
46 return true
47 }
48
49 func tableView(_ tableView:UITableView,
moveRowAt sourceIndexPath:IndexPath, to
destinationIndexPath:IndexPath) {
50 let fromRow = (sourceIndexPath as NSIndexPath).row
51 let toRow = (destinationIndexPath as
NSIndexPath).row
52 let customer = customers[fromRow]
53
54 customers.remove(at:fromRow)
55 customers.insert(customer, at:toRow)
56 }
57 }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容