Set tableView in a ViewController programmatically

时间:2018-09-18 19:42:25

标签: ios swift uitableview uiviewcontroller

I'm trying to set a tableView programmatically inside a viewController (before I worked in a TableViewController, so I kinda want to replace the tableViewController with a normal ViewController with a tableView inside) But I always get nil error messages if I try following code:

class MessagesController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let cellId = "cellId"
private var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.setHidesBackButton(true, animated: false)
    layoutFAB()
    tableView = UITableView(frame:self.view.frame, style: .plain) // or any frame
    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorColor = UIColor.black
    tableView.tableFooterView = UIView()
    tableView.backgroundColor = UIColor(red:0.36, green:0.39, blue:0.40, alpha:1.0)

    navigationController?.navigationBar.tintColor = UIColor(red:1.00, green:0.96, blue:0.02, alpha:1.0)

    let imageRight = UIImage(named: "new_message_icon")
    navigationItem.rightBarButtonItem = UIBarButtonItem(image: imageRight, style: .plain, target: self, action: #selector(handleNewMessage))

    tableView.register(UserCell.self, forCellReuseIdentifier: cellId)

    tableView.allowsMultipleSelectionDuringEditing = true

    self.view.addSubview(tableView)
}

func layoutFAB() {
    let item = FloatyItem()
    item.hasShadow = false
    item.buttonColor = UIColor(red:1.00, green:0.96, blue:0.02, alpha:0.75) //yellow opcaity 0,75
    item.circleShadowColor = UIColor.black
    item.titleShadowColor = UIColor.black
    item.titleLabelPosition = .right
    item.handler = { item in
    }
    floaty.hasShadow = false
    floaty.addItem (icon: UIImage(named: "most-537282")) { item in
        self.navigationController?.popToRootViewController(animated: true)

        self.navigationController?.navigationBar.isHidden = false
    }

    floaty.addItem (icon: UIImage(named: "add")) { item in
        let addVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ComposeViewController") as! ComposeViewController
        self.navigationController?.pushViewController(addVC, animated: true)

        self.navigationController?.navigationBar.isHidden = false
    }

    floaty.addItem (icon: UIImage(named: "setting-512")) { item in
        let settingsVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
        self.navigationController?.pushViewController(settingsVC, animated: true)

        self.navigationController?.navigationBar.isHidden = false
    }


    floaty.paddingX = self.view.frame.width/2 - floaty.frame.width/2
    floaty.fabDelegate = self

    self.view.addSubview(floaty)
}

1 个答案:

答案 0 :(得分:1)

You miss

tableView = UITableView(frame:self.view.frame) // or any frame 
tableView.delegate = self
tableView.dataSource = self

If you want to set style.grouped use this initailizer

tableView = UITableView(frame:self.view.frame,style:.grouped) // or any frame