テーブルのセルがテーブルの幅を埋めていません。以下のコード:テーブルビューでUITableViewCellがテーブルの幅を埋めていない
コントローラー:
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が何もしておらず、イメージがデフォルトサイズで表示されているかのようです。
としてご
cellForRowAtIndexPath
使用この中で、私は "を想定しそのためには「heightForRowAt」を使用します。私は間違っていますか? – steventnorris選択方法を試してください。あなたの配列からいくつかのデータを印刷することができます参照してください。あなたの配列は無限ではありません... – Joe
@Joe配列はセルの配列を参照している場合、nilではありません。それ以外の場合、画像は表示されません。上記のコードを見ると、menuItemsにはセルの構築に使用される配列が含まれており、nilではありません。 – steventnorris