【问题标题】:Set tableView in a ViewController programmatically以编程方式在 ViewController 中设置 tableView
【发布时间】:2018-09-18 19:42:24
【问题描述】:

我正在尝试在 viewController 中以编程方式设置 tableView(在我使用 TableViewController 之前,所以我有点想用普通的 ViewController 替换 tableViewController,里面有一个 tableView) 但是如果我尝试以下代码,我总是会收到 nil 错误消息:

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

【问题讨论】:

  • 你永远不会创建表格视图。
  • 您忘记调用 UITableView 初始化程序之一。
  • nil 到底是什么?
  • tableView.delegate = self @vikingosegundo

标签: ios swift uitableview uiviewcontroller


【解决方案1】:

你错过了

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

如果你想设置 style.grouped 使用这个初始化器

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

【讨论】:

  • 这不是 UITableView 的正确初始化程序。你需要一个带有框架和样式的。
  • @rmaddy 我认为 plain 是默认的
  • 好的,现在表格视图显示出来了,但我看不到我的 FloatingButton,所以请告诉我你说的其他框架是什么意思?
  • 什么浮动按钮??导航右键?? ,我的意思是将表格的框架设置为您想要编辑的任何框架 CGRect
  • 对于浮动按钮,我使用了“self.view.addSubview(floaty)”,但它不会再显示在 tableview 上
猜你喜欢
  • 2017-07-04
  • 2020-07-03
  • 2019-03-18
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多