2015-09-06 134 views
2

我想添加一个自定义的UITableViewCell到我的分割视图控制器主。当我运行我的应用程序时,单元格不显示,尽管它们确实带有由println语句确认的信息。经过一番研究,我发现只有标准的UITableViewCells被生成。分割视图控制器 - 自定义UITableViewCell

Table ViewController具有通过故事板连接的数据源和委托,并且UITableViewCell具有带标识符的自定义类。

TableView & TableViewCell在同一个ViewController中。

TableViewCell可重用标识符是“ArtistCell”,它的类是ArtistCell。

import UIKit 
import MediaPlayer 

class ArtistsMasterTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource { 
    var artistsQuery:MPMediaQuery! 
    var artists:NSArray! 
    var artistAlbums:NSArray! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     artistsQuery = MPMediaQuery.artistsQuery() 
     artists = artistsQuery.collections 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return artists.count 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     println(artists[indexPath.row].items) 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("ArtistCell", forIndexPath: indexPath) as! ArtistCell 

     cell.backgroundColor = UIColor.clearColor() 

     var rowItem:MPMediaItem = artists.objectAtIndex(indexPath.row).representativeItem 

     if let artwork = rowItem.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork { 
      cell.artistImage!.image = artwork.imageWithSize(cell.artistImage.bounds.size) 
     } 

     cell.artistNameLabel.text = rowItem.valueForProperty(MPMediaItemPropertyArtist) as? String 
     //  artistCell.albumsLabel.text = rowItem.valueForProperty(MPMediaItemPropertyArtist) as? String 
     //  artistCell.songsLabel.text = rowItem.valueForProperty(MPMediaItemPropertyArtist) as? String 

     return cell 

} 

的自定义单元格:

class ArtistCell: UITableViewCell { 

    @IBOutlet weak var artistImage: UIImageView! 
    @IBOutlet weak var artistNameLabel: UILabel! 
    @IBOutlet weak var albumsLabel: UILabel! 
    @IBOutlet weak var songsLabel: UILabel! 

} 

这里有一些故事板截图: The Table View

The Outlets Inspector

The Attributes Inspector

The Storyboard hierarchy

+0

您是否将单元格设计为同一个故事板文件中的表格视图的原型单元格? – hennes

+0

是的,细胞是一个原型,并在同一个故事板。 –

+0

它们是否也在同一个ViewController中? –

回答

4

伟大的问题。从我所看到的实现这个的方式是创建一个您出队的定制xib文件。这里是在Objective-C对象最佳文章:https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722

从本质上讲,你执行以下操作:

  1. 设置你的表一样,你必须减去可重复使用的电池
  2. 创建一个自定义的XIB文件并使它看起来像你的原型细胞(你可以简单地做到这一点通过使一个空的对象,并拖动tableviewcell到故事板

​​

  • 确保类类型的XIB的设置为ArtistCell在Identity检查
  • enter image description here

  • 创建自定义类ArtistCell钩住XIB高达
  • enter image description here

  • 当你万一设置你的cellForRowAtIndexPath

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? ArtistCell 
    
    if cell == nil{ 
        tableView .registerNib(UINib(nibName: "ArtistCell", bundle: nil), forCellReuseIdentifier: "cell") 
        cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? ArtistCell 
    } 
    
    cell?.nameLabel.text = "Hello World" 
    
    return cell! 
    } 
    
  • 这里有一个屏幕盖使用此代码:

    enter image description here

    此代码已被全面的测试和对我的作品。仅供参考。 :-) 希望这可以帮助!

    干杯!
    Rob Norback

    相关问题