使用Swift编程动态创建UITableView无法加载

9
我正在尝试使用Swift以编程方式创建一个uitableview,但它没有加载。这是我的代码:
class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
    var tableView: UITableView  =   UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate      =   self
        tableView.dataSource    =   self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView)
    }

你们中有人知道我错过了什么或我的代码有什么问题吗?

我真的很感谢你们的帮助。


你需要使用适当的初始化器来创建表格视图。使用那个指定类型和框架的初始化器。 - rmaddy
@rmaddy,你能发一个例子吗? - user2924482
7个回答

10
override func viewDidLoad() {
    super.viewDidLoad()
    tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
    tableView.delegate      =   self
    tableView.dataSource    =   self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.view.addSubview(self.tableView)
}

tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)

这行代码将设置你的tableview的框架,同时使用默认的UITableViewStyle


如果你没有设置frame,即使在你的UITableView代码中其他所有内容都正确,也不会有任何显示。tableView.frame = UIScreen.mainScreen().bounds就可以解决这个问题。 - mythicalcoder

1

SWIFT 3.x

//MARK: Create TableView

func createTableView()   -> Void {

    print("Create Table View.")
    if self.tblView == nil{
        self.tblView = UITableView(frame: UIScreen.main.bounds, style: .plain)
        self.tblView.delegate = self
        self.tblView.dataSource = self
        self.tblView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tblView)
    }
    else{
        print("Table view already has been assigned.")
    }
}

1
晚了但可以帮助其他人,你需要添加:

tableView.translateAutoresizingMaskIntoConstraints = false

这里出错了。类型 'UITableView' 的值没有成员 'translateAutoResizingMaskIntoConstraints' !! - mythicalcoder
translatesAutoresizingMaskIntoConstraints - Amr Lotfy

1

试试这个:

 override func viewDidLoad() {
    super.viewDidLoad() 
    let screenSize:CGRect = UIScreen.mainScreen().bounds

    tableView.frame = CGRectMake(0, 0, screenSize.width, screenSize.height)
    tableView.delegate      =   self
    tableView.dataSource    =   self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.view.addSubview(self.tableView) 
}

你可以自定义框架,如:CGRectMake(x,y,width,height) 这里的width和height指的是所需的tableview宽度和高度。

0

SWIFT 3:

var tableView: UITableView  =   UITableView()

override func viewDidLoad() {
        super.viewDidLoad()

      tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
      tableView.delegate = self
      tableView.dataSource = self
      tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
       self.view.addSubview(tableView)
}

0

在设置委托之前,请使用

tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)

它将正常工作


0

SWIFT 3x

tableView.register(UINib(nibName: "cell", bundle: nil), forCellReuseIdentifier: "cell")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接