前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS UITableViewCell 移动单元格位置

IOS UITableViewCell 移动单元格位置

作者头像
用户5760343
发布2019-07-08 11:37:20
1.3K0
发布2019-07-08 11:37:20
举报
文章被收录于专栏:sktjsktj
 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档