【问题标题】:Can't add UITableView to UIView swift无法将 UITableView 添加到 UIView swift
【发布时间】:2016-06-19 00:30:54
【问题描述】:

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

我在 tableview (...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
    }
}

【问题讨论】:

  • @Nch-just set frame for UItableview .write tableView.frame = CGRect(x: 0, y: 0, width: 320, height: 568) 在你的 viewDidLoad 方法中 希望它能正常工作你..

标签: ios iphone swift uitableview uiview


【解决方案1】:

我想你忘了添加

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

【讨论】:

  • 您在该位置创建视图的位置添加此代码
  • 谢谢!当我添加 tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain) 它崩溃但使用 tableView = UITableView() 时,我的 tableView 显示,但它是空的。我正在使用 Cell.swift 类中的自定义单元格,我以这种方式注册它,它在 UITableViewController: tableView.registerClass(Cell.self, forCellReuseIdentifier: NSStringFromClass(Cell)) 中工作,我不再工作了'已经将它嵌入到 UIViewController 中。对此有何想法?
  • cellArray 不为空,我可以打印它的数据,但是我的 Cell 类没有收到它。
  • 勾选选项-3答案
猜你喜欢
  • 2012-03-05
  • 2019-05-09
  • 1970-01-01
  • 2016-02-29
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
相关资源
最近更新 更多