我的表格单元格没有填满表格的宽度。代码如下:
带表格视图的控制器:
import UIKit
class MenuController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var menuTable:UITableView!
var rootNav:UINavigationController!
private var cellIdentifier = "MenuCell"
//This is an array of arrays of dictionaries with String key and Any value
//Top level array is sections
//Second level array is items
//Dictionary holds data for that individual cell
private final var menuItems:[[[String:Any?]]] = [
//Section 1
[
//Shot Chart
["image":Images.BALL,"title":" Chart","stack":ViewStacks.CHART],
//Profile
["image":Images.PROFILE,"title":"Profile","stack":ViewStacks.PROFILE]
],
//Section 2
[
//Logout
["image":Images.LOGOUT,"title":"Logout","stack":nil]
]
]
init(rootNav:UINavigationController, nibName:String?, bundle:Bundle?) {
self.rootNav = rootNav
super.init(nibName: nibName, bundle: bundle)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Table View Datasource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:MenuCell? = menuTable.dequeueReusableCell(withIdentifier: cellIdentifier) as! MenuCell?
if cell == nil {
cell = MenuCell(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
}
let cellProperties = menuItems[indexPath.section][indexPath.row]
cell!.imageView?.image = (cellProperties["image"] as! UIImage)
cell!.titleLbl?.text = (cellProperties["title"] as! String)
return cell!
}
func numberOfSections(in tableView: UITableView) -> Int {
return menuItems.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems[section].count
}
//Table View Delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}
的UITableViewCell:
import UIKit
class MenuCell: UITableViewCell {
@IBOutlet var iconImg:UIImageView!
@IBOutlet var titleLbl:UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
super.setSelected(false, animated: true)
}
}
图像显示,但没有文字,没有背景,没有。好像autoLayout什么也不做,图像只是显示在它的默认大小。
答案 0 :(得分:0)
因为你已经制作了一个UITableViewCell
的xib而没有注册。
它正在显示图像,因为您使用cell.imageView
这是UITableViewCell
的默认imageView,它始终满足nil条件。您尚未提供图像视图的任何插件,您已将其拖放到UITableViewCell
xib中。
所以改变你的viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
menuTable.register(UINib(nibName: "MenuCell", bundle: nil), forCellReuseIdentifier: "MenuCell")
}
确保您提供的UITableViewCell
子类MenuCell
的标识符为 MenuCell
同时将cellForRow
方法更改为
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! MenuCell
let cellProperties = menuItems[indexPath.section][indexPath.row]
// you are using imageView which is the default property for UITableViewCell. Create your own imageView outlet. Name it as menuImageView and replace imageView with menuImageView
cell.imageView?.image = (cellProperties["image"] as! UIImage)
cell.titleLbl?.text = (cellProperties["title"] as! String)
return cell
}
答案 1 :(得分:0)
正如@Rajan在他的回答问题中提到的那样,不是你的布局。所以只需将此函数放入customTableViewCell类
即可 class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> MenuCell TableViewCell {
let k MenuCell TableViewCellIdentifier = "kMenuCellTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "MenuCellTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kMenuCellTableViewCellIdentifier)
let cell = tableView.dequeueReusableCellWithIdentifier(kMenuCellTableViewCellIdentifier, forIndexPath: indexPath) as! MenuCellTableViewCell
return cell
}
并在cellForRowAtIndexPath
中将其用作
let cell = MenuCellTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor() // Do something with your cell