Tableviewcontroller不显示单元格

时间:2016-10-24 15:47:11

标签: swift tableview

我在storyborad中创建了表格。但我从代码中将其归化。问题是:当我从代码中将其初始化时它不显示单元格,我的单元格是静态的

enter image description here

这是我的tableview控制器代码:

class AutorizedProfileTableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundView=UIImageView(image: #imageLiteral(resourceName: "bg_profile"))
    //tableView.backgroundColor=UIColor(colorLiteralRed: 69/255, green: 69/255, blue: 69/255, alpha: 0.3)
    tableView.tableFooterView=UIView()
    tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 7
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0{
        return 250
    }
    else{
        return 50
    }
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor=UIColor.clear
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

这就是我如何通过代码来实现它:

if (UIApplication.shared.delegate as! AppDelegate).currentcookie[HTTPCookiePropertyKey.value] != nil{
            let menuTable=AutorizedProfileTableViewController().self
            let size=UIScreen.main.bounds.size
            menuTable.view.frame=CGRect(x: size.width-(size.width*3/4), y: 0, width: size.width*3/4, height: size.height)
            profile.addSubview(menuTable.view)
        }else{
            let menuTable=NoAutorizedProfileTableViewController()
            let size=UIScreen.main.bounds.size
            menuTable.view.frame=CGRect(x: size.width-(size.width*3/4), y: 0, width: size.width*3/4, height: size.height)
            profile.addSubview(menuTable.view)
        }
        self.navigationController?.view.addSubview(profile)
        menuOpen=menuOpen ? false : true
        if menuOpen{
            self.navigationController?.view.isUserInteractionEnabled=false
            self.navigationController?.view.bringSubview(toFront: profile)
        }
        else{
            self.navigationController?.view.isUserInteractionEnabled=true
            profile.removeFromSuperview()
        }

什么是问题,为什么我看不到我的细胞?

1 个答案:

答案 0 :(得分:0)

你错过了

 self.tableview.delegate = self
 self.tableview.datasource = self

在viewDidLoad