首页
学习
活动
专区
圈层
工具
发布

IOS UITableViewCell 移动单元格位置

代码语言:javascript
代码运行次数:0
复制
 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 }
举报
领券