Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否删除动态调整表视图的行?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 是否删除动态调整表视图的行?

Ios 是否删除动态调整表视图的行?,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试删除tableview中的一行。删除该行时,应调整tableview,以便tableview中没有空行。这是删除任何行之前的情况: 这是即将删除行时的外观: 这是删除行后的外观: 删除第四行后,不应该有额外的空行。我怎样才能摆脱这一排多余的人 这是我目前的代码。如您所见,tableview的大小是动态调整的。tableview的总体大小根据是否有4个项目(如第一幅图所示)或是否有6个项目而有所不同。在这两种情况下,在进行任何删除之前,表中没有多余的行(因此第一种情况下总共只有4行

我正在尝试删除tableview中的一行。删除该行时,应调整tableview,以便tableview中没有空行。这是删除任何行之前的情况:

这是即将删除行时的外观:

这是删除行后的外观:

删除第四行后,不应该有额外的空行。我怎样才能摆脱这一排多余的人

这是我目前的代码。如您所见,tableview的大小是动态调整的。tableview的总体大小根据是否有4个项目(如第一幅图所示)或是否有6个项目而有所不同。在这两种情况下,在进行任何删除之前,表中没有多余的行(因此第一种情况下总共只有4行,第二种情况下总共只有6行)。此外,按钮应该位于tableview结尾下方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]
}

只需在ViewDodLoad方法中添加以下内容

    tableView.tableFooterView = UIView()
现在将不会有额外的空行

下一步是

将高度约束添加到tableview,并对其进行调整

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

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


希望它有帮助

您不应该直接调用视图生命周期方法,例如
viewdilayoutsubviews
。此外,直接设置帧也不理想。您应该在项目之间设置约束,并让autolayout移动项目。删除后重新加载整个tableview也会产生负面的视觉影响,尤其是当tableview变大时。我知道约束可以确保按钮始终低于tableview末尾80像素。自动布局/约束是否有办法解决行删除问题?您需要约束tableview的高度,然后根据需要更新该约束的
常量。您可能应该查看tableview的
contentSize
属性,而不是它的框架。这很有效!我为tableview的高度添加了一个约束,然后更新了该约束。谢谢。另外,在您的情况下,是否要显示TableViewCell的“惊人”部分,以便在即将删除该行时可见?