【问题标题】:Swift UITableView custom Cell ordering Drag Drop?Swift UITableView 自定义单元格排序拖放?
【发布时间】:2017-09-08 08:34:37
【问题描述】:

我正在尝试通过长按来使用UITableView 单元格,然后拖放该单元格,当我使用自定义单元格视图时它不起作用,知道为什么!

这是 TableView 视图控制器:

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // Then delegate the TableView
        self.tableView.delegate = self
        self.tableView.dataSource = self

        // Register table cell class from nib
        let bundle       = Bundle(for: type(of: self))
        let cellNib = UINib(nibName: "tbc_song_vertical", bundle: bundle)
        self.tableView.register(cellNib, forCellReuseIdentifier: "tbc_song_vertical")

        //Loading Template
        let nib_tbc_loading = UINib(nibName: "tbc_loading", bundle: bundle)
        self.tableView.register(nib_tbc_loading, forHeaderFooterViewReuseIdentifier: "tbc_loading")

        //Automated ell height
        self.tableView.estimatedRowHeight = 44.0
        self.tableView.rowHeight          = UITableViewAutomaticDimension
        self.tableView.reloadData()

        self.tableView.isEditing = true
    }

    // MARK: - Sorting

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

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

     func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let movedObject = self.data[sourceIndexPath.row]
            data.remove(at: sourceIndexPath.row)
            data.insert(movedObject, at: destinationIndexPath.row)

        // To check for correctness enable: self.tableView.reloadData()
    }

    //loading footer
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView    = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "tbc_loading") as! tbc_loading
        footerView.startAnimate()
        return footerView
    }

    //loading footer
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

        return ( self.is_fetching ) ? 40 : 0
    }

    //Pagination
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

        //Bottom Refresh
        if scrollView == tableView{

            if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height )
            {

            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell : tbc_song_vertical = tableView.dequeueReusableCell(withIdentifier: "tbc_song_vertical", for: indexPath) as! tbc_song_vertical

           cell.fillwithInfo(dto: self.data[indexPath.row] )

        return cell
    }

    // Tabbed Cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { }

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

}

仅当我使用自定义视图时它不起作用,但是当我将手指放在任何单元格的右侧以向上或向下拖动它时,它会拖动所有表格

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    添加如下方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2015-08-26
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多