Swift 简单的UITableView排序

懒的话可以直接download:Swift TableView排序
代码:
var tableView : UITableView! = nil
var dataArray : NSMutableArray = []

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.initData()
    self.makeUI()
}

func edit(){
    self.tableView.setEditing(!self.tableView.isEditing, animated: true)
}

func initData(){
    for idx in 0..<100 {
        dataArray.add("\(idx)")
    }
}

func makeUI(){
    
    self.view.backgroundColor = UIColor.white
    self.title = "拖动cell排序"
    self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.edit, target: self, action: #selector(ViewController.edit))
    
    let tabView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height), style: .plain)
    view.addSubview(tabView)
    tabView.register(UITableViewCell.self, forCellReuseIdentifier: "IDD")
    tabView.delegate = self
    tabView.dataSource = self
    self.tableView = tabView
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataArray.count
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "IDD", for: indexPath)
    cell.textLabel?.text = self.dataArray[indexPath.row] as? String
    return cell
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle.none
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    dataArray.exchangeObject(at: sourceIndexPath.row, withObjectAt: destinationIndexPath.row)
}

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 45.0
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 15.0
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.1
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}
pic.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,500评论 4 61
  • 我们只接受自己认为配得上的爱 晓晓得新男友据说是法律精英,小有资产,自己还有家贸易公司。 下班楼下第一次看到了他,...
    关自渡阅读 2,033评论 0 2
  • 首先,我不得不声明,这篇文章并不属于文笔优美或是惺惺作态,告诉读者孤独的意义的文章。如果读者有意,可将本文视作一位...
    老老老酒阅读 358评论 9 1
  • 食材:面粉300克 玉米50克 黑米50克 藜麦50克 荞麦50克 酵母5克 温水250克 方法: 1、用料理机将...
    素之味阅读 725评论 0 0
  • 网海无边,秘密花园。 时光逆转,记忆重现。 诗情天地,共绘美集。 远方不远,同享温暖。 图片发自简书App 如花岁...
    窗花阅读 248评论 0 2

友情链接更多精彩内容