为什么我的UITableViewController页脚高度不会改变?

问题描述 投票:0回答:2

我正在Swift Swiftgrounds中编写一个模拟消息传递应用程序,为了保存消息,我有一个带有自定义单元格的UITableViewController。我正在尝试向表格添加“粘性”页眉和页脚,以分别保存导航控件,消息输入和发送按钮。我似乎无法更改页眉和页脚的高度。我曾尝试使用tableView(_:heightForFooterInSection:)tableView(_:heightForHeaderInSection:),但这也不起作用。

这是我到目前为止的内容:

import UIKit
import PlaygroundSupport

class ViewController: UITableViewController {

    var textMessages = [
        "Here's my very first message",
        "I'm going to message another long message that will word wrap",
        "I'm going to message another long message that will word wrap, I'm going to message another long message that will word wrap, I'm going to message another long message that will word wrap",
        "some messaging"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Messages"
        navigationController?.navigationBar.prefersLargeTitles = true

        tableView.register(ChatMessageCell.self, forCellReuseIdentifier: "cell_1")
        tableView.separatorStyle = .none

        adddatextbruh()

    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
        let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 75))
        customView.backgroundColor = UIColor.red
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 75))
        button.setTitle("Submit", for: .normal)
        //button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        customView.addSubview(button)
        return customView
    }

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?{
        let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 75))
        customView.backgroundColor = UIColor.red
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 75))
        button.setTitle("Submit", for: .normal)
        //button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        customView.addSubview(button)
        return customView
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return textMessages.count
    }

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

        if indexPath.row % 2 == 1{
            cell.messageLabel.text = textMessages[indexPath.row]
            //cell.setupConstraints(side: 1)
            cell.bubbleBackgroundView.backgroundColor = UIColor(white: 0.9, alpha: 1)
            return cell
        }else{
            cell.messageLabel.text = textMessages[indexPath.row]
            //cell.setupConstraints(side: 0)
            cell.bubbleBackgroundView.backgroundColor = .blue
            return cell
        }
    }
    //let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ChatMessageCell
    //        cell.textLabel?.text = "We want to provide a longer string that is actually going to wrap onto the next line and maybe even a third line."
    //        cell.textLabel?.numberOfLines = 0
    func adddatextbruh(){
        textMessages.append("hey whats up")
        tableView.beginUpdates()
        tableView.insertRows(at: [
            (NSIndexPath(row: textMessages.count-1, section: 0) as IndexPath)], with: .automatic)
        tableView.endUpdates()
        tableView.scrollToRow(at: IndexPath(row: textMessages.count-1, section: 0), at: UITableView.ScrollPosition.bottom, animated: true)
    }
}




class ChatMessageCell: UITableViewCell {

    let messageLabel = UILabel()
    let bubbleBackgroundView = UIView()
    var leadingAnchorConstant = CGFloat()

    func setupConstraints(side: Int){
        if side == 1{
            leadingAnchorConstant = frame.size.width - 176
        }else{
            leadingAnchorConstant = 32
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        bubbleBackgroundView.backgroundColor = .yellow
        let gradient = CAGradientLayer()

        bubbleBackgroundView.layer.shadowOpacity = 0.35
        bubbleBackgroundView.layer.shadowRadius = 6
        bubbleBackgroundView.layer.shadowOffset = CGSize(width: 0, height: 0)
        bubbleBackgroundView.layer.shadowColor = UIColor.black.cgColor

        bubbleBackgroundView.layer.cornerRadius = 25
        bubbleBackgroundView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(bubbleBackgroundView)

        addSubview(messageLabel)
        //        messageLabel.backgroundColor = .green
        messageLabel.text = "We want to provide a longer string that is actually going to wrap onto the next line and maybe even a third line."
        messageLabel.numberOfLines = 0

        messageLabel.translatesAutoresizingMaskIntoConstraints = false

        // lets set up some constraints for our label
        let constraints = [messageLabel.topAnchor.constraint(equalTo: topAnchor, constant: 32),
                           messageLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 32),
                           messageLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -32),
                           messageLabel.widthAnchor.constraint(equalToConstant: 250),

                           bubbleBackgroundView.topAnchor.constraint(equalTo: messageLabel.topAnchor, constant: -16),
                           bubbleBackgroundView.leadingAnchor.constraint(equalTo: messageLabel.leadingAnchor, constant: -16),
                           bubbleBackgroundView.bottomAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 16),
                           bubbleBackgroundView.trailingAnchor.constraint(equalTo: messageLabel.trailingAnchor, constant: 16),
        ]

        NSLayoutConstraint.activate(constraints)

        //        messageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

PlaygroundPage.current.liveView = ViewController()
ios swift uikit swift-playground
2个回答
0
投票

已解决。我只需在viewDidLoad()中设置tableView.sectionFooterHeight = //the height。不知道为什么heightForFooterInSection / headerInSection无法正常工作,但是对我来说,这要干净得多。


0
投票

不是高度的跟随功能

© www.soinside.com 2019 - 2024. All rights reserved.