首页 > 解决方案 > 删除动态调整表视图的行?

问题描述

我正在尝试删除表格视图中的一行。当行被删除时,tableview 应该进行调整,使 tableview 中没有空白行。这是删除任何行之前的样子:

在此处输入图像描述

这是即将删除一行时的样子:

在此处输入图像描述

这是删除一行后的样子:

在此处输入图像描述

删除第四行后不应该有多余的空白行。我如何摆脱这个多余的行?

这是我目前的代码。如您所见,tableview 的大小是动态调整的。根据是否有 4 个项目(如第一张图所示)或是否有 6 个项目,tableview 的整体大小是不同的。在这两种情况下,在发生任何删除之前,表中都没有多余的行(因此第一种情况总共只有 4 行,第二种情况总共只有 6 行)。此外,按钮应该在表格视图结尾下方 80 像素处。当删除一行时,按钮正确移动,您可以看到按钮已从图像 2 向上移动到 3。但是,它看起来仍然在 tableview 中有一个额外的行。

@IBOutlet var tableView: UITableView!

@IBOutlet var newButton: UIButton!

var items: [String] = ["Swift", "Is", "So", "Amazing"]

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as! UITableViewCell

    // Make sure the table view cell separator spans the whole width
    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero

    cell.textLabel?.text = self.items[indexPath.row]

    return cell
}

override func viewWillAppear(_ animated: Bool) {
    // Adjust the height of the tableview
    tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)

    // Add a border to the tableView
    tableView.layer.borderWidth = 1
    tableView.layer.borderColor = UIColor.black.cgColor
}

// This function is used for adjusting the height of the tableview
override func viewDidLayoutSubviews(){
    tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
    tableView.reloadData()

    //Get the current height of the tableview
    var tableViewHeight = self.tableView.contentSize.height
    var tableViewEnding = 134 + tableViewHeight
    var buttonPlacement = tableViewEnding + 80

    // The New Button is 80 points below the ending of the tableView
    newButton.frame.origin.y = buttonPlacement

    print("Table View Height: \(tableViewHeight)")
}

// Allow cell deletion in tableview
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
        // delete item at indexPath
        self.items.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        self.viewDidLayoutSubviews()
        print(self.items)
        print("Number of rows: \(tableView.numberOfRows(inSection: 0))")
    }

    delete.backgroundColor = UIColor.blue
    return [delete]
}

标签: iosswiftuitableview

解决方案


只需在视图中添加以下内容加载方法

    tableView.tableFooterView = UIView()

现在不会有多余的空行了。

下一步是

将高度约束添加到 tableview 并从中获取 IBOutlet。

在 tableview 的任何更新后,使用 tableview 设置约束的常量值contentSize.height

注意:如果您的单元格有一些繁重的任务要做,那么在重新加载数据旁边设置常量可能无法正常工作,在这种情况下您可以尝试 viewDidLayoutSubviews 方法

希望对您有所帮助


推荐阅读