Назначить верхнюю границу для высоты таблицы? - PullRequest
0 голосов
/ 17 мая 2018

У меня есть таблица, высота которой динамически регулируется. Высота регулируется таким образом, чтобы табличное представление включало только определенное количество строк, а не пустых строк. Таким образом, если есть только 2 строки, то высота меньше, а если 4 строки, то высота больше. Однако я не хочу, чтобы его высота проходила мимо определенной точки. Как я могу позволить динамически регулировать высоту, не позволяя высоте когда-либо достигать определенной точки?

Это мой код:

@IBOutlet var tableView: UITableView!

@IBOutlet var tableViewHeightConstraint: NSLayoutConstraint!

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
    print("We ran ViewWillAppear")
}

// 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()
}

// 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.tableViewHeightConstraint.constant = self.tableView.contentSize.height - 44
        print(self.items)
        print("Number of rows: \(tableView.numberOfRows(inSection: 0))")
    }

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

1 Ответ

0 голосов
/ 17 мая 2018

В viewWillAppear, вы можете попробовать

let limit:CGFloat = 900.0

if tableView.contentSize.height  < limit {
    tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
}
else {
    tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: limit )
}

Или установить значение

tableViewHeightConstraint.constant = ( tableView.contentSize.height < limit ) ? tableView.contentSize.height : limit

self.view.layoutIfNeeded()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...