无法将 UITableView 添加到 UIView swift



我有一个带有标题子视图和UITableViewUIViewController。当我在UITableView类上实现它时,我的 tableView 及其自定义单元格可以完美运行,但是一旦我尝试将其嵌入我的UIViewController中,我就会根据不同的背景颜色获得我看到的表视图框架,但它没有填充我的自定义单元格。

我在表格视图中的打印语句(...cellForRowAtIndexPath..)不打印任何内容,就好像没有调用该函数一样。

不确定我做错了什么,任何帮助都非常感谢!

import UIKit
class ProfileViewController: UIViewController , UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView! 
var header: UIView!
var userName: UILabel!
var userImage: UIImageView!
var dataDict : NSDictionary!
var cellArray: NSArray!
var friends = [Friends]()
init(dataDict: NSDictionary , nibName: String?, bundle: NSBundle?){
    super.init(nibName: nibName, bundle: bundle)
    self.dataDict = dataDict
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
    super.viewDidLoad()
    cellArray = dataDict?.objectForKey("data") as! NSArray
    tableView.delegate = self
    tableView.dataSource = self
    header   = UIView(frame: CGRectZero)
    header.backgroundColor =  UIColor( red:77/255.0, green:255/255.0, blue:195/255.0, alpha:1.0)
    userName = UILabel(frame: CGRectZero)
    userName.textAlignment = .Left
    userName.text = "Nachwa El khamlichi"
    userName.textColor = UIColor.blackColor()
    userName.frame = CGRectMake(10, 100, self.view.frame.width/2, 40)
    userImage  = UIImageView(frame: CGRectZero)
    userImage.frame = CGRectMake(10, 30, 80, 80)
    tableView.contentInset = UIEdgeInsets(top: 300, left: 0, bottom: 0, right: 0)
    tableView.backgroundColor = UIColor.orangeColor()
    tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
    self.view.addSubview(header)
    self.view.addSubview(userName) 
    self.view.addSubview(userImage)
    self.view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellArray.count
    }
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        print(cellArray.count)
        let cell = tableView.dequeueReusableCellWithIdentifier( NSStringFromClass(Cell), forIndexPath: indexPath) as! Cell
        let friendName = cellArray[indexPath.row]["name"] as? String
        let avatarId   = cellArray[indexPath.row]["id"] as? String
        friends.append(Friends(name: friendName!, avatarId: avatarId!))
        cell.friend?.name =  friendName
        cell.friend = friends[indexPath.row]
        return cell
    }
     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 40
    }
}

我想你忘了添加

    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)

同时你忘记添加框架

  header   = UIView(frame: CGRectZero)
  header.frame = CGRectMake(10, 100, self.view.frame.width/2, 40)
  header.backgroundColor =  UIColor( red:77/255.0, green:255/255.0, blue:195/255.0, alpha:1.0)

操作-2

如果一切都很好,请检查一次 您的cellArray包含可用或不可用的数据",

选项-3

在此行之后添加此

   self.view.addSubview(self.tableView)
    tableView.reloadData()

最新更新