Ios 以编程方式在ViewController中设置tableView
在使用TableViewController之前,我尝试在viewController中以编程方式设置tableView,因此我有点想用普通的viewController替换TableViewController,并在其中使用tableView 但如果我尝试以下代码,则始终不会收到任何错误消息:Ios 以编程方式在ViewController中设置tableView,ios,swift,uitableview,uiviewcontroller,Ios,Swift,Uitableview,Uiviewcontroller,在使用TableViewController之前,我尝试在viewController中以编程方式设置tableView,因此我有点想用普通的viewController替换TableViewController,并在其中使用tableView 但如果我尝试以下代码,则始终不会收到任何错误消息: class MessagesController: UIViewController, UITableViewDataSource, UITableViewDelegate { let cellId
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)
}
你错过了
tableView = UITableView(frame:self.view.frame) // or any frame
tableView.delegate = self
tableView.dataSource = self
如果要设置style.grouped,请使用此initailizer
tableView = UITableView(frame:self.view.frame,style:.grouped) // or any frame
您从未创建过表视图。您忘记调用其中一个UITableView初始值设定项。什么是nil?tableView.delegate=self@VikingoSegundo这不是UITableView的正确初始值设定项。您需要一个带有框架和样式的框架。@rmaddy我认为“普通”是默认设置。现在显示tableview,但我再也看不到我的浮动按钮,所以请告诉我其他框架是什么意思?什么浮动按钮??导航右键,我的意思是将表格的框架设置为我使用self.view.addSubviewfloaty的浮动按钮想要查看的任何框架,但它将不再显示在表格视图上