【问题标题】:TableView row height shrinks when I am trying to reorder row using tableView(moveRowAt:)当我尝试使用 tableView(moveRowAt:) 重新排序行时,TableView 行高缩小
【发布时间】:2019-08-28 07:45:26
【问题描述】:

我正在使用自定义 tableViewCell 来填充我的 tableView

1. title = UILabel()
2. subtitle = UITextView()
3. count = UILabel()

这些都是使用autoLayoutConstraints 以编程方式设置的。

然后,我将在我的主 viewController 中定义的 tableView 填充为 tableView datasourcetableView delegate。所以所有与tableView 相关的设置都发生在我的主视图控制器中。

我正在使用automaticDimension 来设置tableViewrowHeight

问题是,

当我使用 func tableView(moveRowAt:) 重新排列我的 tableView 行时,当我将选定的 tableView 行拖到另一个 indexPath 时,行高会缩小或折叠。

我该如何解决这个问题?

在课堂上MainViewController: UIViewController

    lazy var tableView: UITableView = {
        let tv = UITableView()
        tv.delegate = self
        tv.dataSource = self
        tv.separatorStyle = .none
        tv.rowHeight = UITableView.automaticDimension
        tv.backgroundColor = nil
        tv.allowsMultipleSelectionDuringEditing = true
        tv.allowsSelection = true
        tv.estimatedRowHeight = 70
        return tv
    }()

【问题讨论】:

  • 行减少到等于estimatedRowHeight的高度?

标签: ios swift uitableview


【解决方案1】:

好的,问题是我在 CustomCell.swift 中添加了这行代码

    override var frame: CGRect {
        get {
            return super.frame
        }
        set (newFrame) {
            var frame =  newFrame
            frame.origin.y += 4
            frame.size.height -= 6
            super.frame = frame
        }
    }

这行代码增加了行之间的间距,但是当试图在编辑模式下移动行时,它会以某种方式干扰行高。

必须将其删除并重构为更合适的代码,以便在行之间实现适当的间距。

【讨论】:

    【解决方案2】:

    您可以将此代码用于 moveRowAt indexpath 功能

    class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    
    
        var animals = ["Dog","Cat","Mouse","hen"]
        var numbers = ["1","2","3","4"]
        @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.tableView.isEditing = true
            tableView.rowHeight = UITableView.automaticDimension
            tableView.estimatedRowHeight = 160
            // tableView.allowsMultipleSelectionDuringEditing = true
            //tableView.allowsSelection = true
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return animals.count
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TableViewCell
            cell.animalOne.text = animals[indexPath.row]
            cell.NumberOne.text = numbers[indexPath.row]
            cell.backgroundColor = UIColor.green
    
            return cell
        }
        func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
            let moveobject = self.animals[sourceIndexPath.row]
            animals.remove(at: sourceIndexPath.row)
            animals.insert(moveobject, at: destinationIndexPath.row)
        }
    
    }
    

    在表格视图单元中

    @IBOutlet weak var textView: UITextView!
        @IBOutlet weak var animalOne: UILabel!
        @IBOutlet weak var NumberOne: UILabel!
    

    【讨论】:

    • 感谢您的评论,但这不是我的问题
    猜你喜欢
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多