2016-10-27 4 views
1

テーブルのセルがテーブルの幅を埋めていません。以下のコード:テーブルビューで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) 
    } 
} 

細胞上の制約: enter image description here

のようになりますどのような細胞: enter image description here

イメージは表示されますが、テキストは表示されず、背景も何も表示されません。これは、autoLayoutが何もしておらず、イメージがデフォルトサイズで表示されているかのようです。

+0

としてごcellForRowAtIndexPath使用この中で、私は "を想定しそのためには「heightForRowAt」を使用します。私は間違っていますか? – steventnorris

+0

選択方法を試してください。あなたの配列からいくつかのデータを印刷することができます参照してください。あなたの配列は無限ではありません... – Joe

+0

@Joe配列はセルの配列を参照している場合、nilではありません。それ以外の場合、画像は表示されません。上記のコードを見ると、menuItemsにはセルの構築に使用される配列が含まれており、nilではありません。 – steventnorris

答えて

0

UITableViewCellのxibを作成し、それを登録していないためです。

UITableViewCellのデフォルトのimageViewであるcell.imageViewを使用しており、常にnil条件を満たしているため、画像が表示されています。 UITableViewCell xibにドラッグ&ドロップしたimageViewのコンセントは提供していません。

もそう

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 
} 
0

ようにあなたのcellForRow方法を変更

MenuCellあなたがMenuCellつまり、あなたのUITableViewCellサブクラスを与えている識別子がであることを確認し、あなたの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") 
} 

を変更@ 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 
} 

を、この機能を入れて、この場合には、常に50でなければなりませんあなたの答えアドレスのみの高さ、@Joe

let cell = MenuCellTableViewCell.cellForTableView(tableView, atIndexPath: indexPath) 
cell.backgroundColor = UIColor.clearColor() // Do something with your cell 
関連する問題