拆分视图控制器 - 自定义UITableViewCell

时间:2015-09-06 13:02:14

标签: ios swift uitableview uisplitviewcontroller

我正在尝试将自定义UITableViewCell添加到我的Split View Controller Master。当我运行我的应用程序时,单元格不会出现,尽管它们确实包含了println语句确认的信息。经过一些研究,我发现只生成了标准的UITableViewCells。

Table ViewController具有通过Storyboard连接的数据源和代理,UITableViewCell具有带标识符的自定义类。

TableView& TableViewCell位于同一个ViewController中。

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

 language: 'es'

自定义单元格:

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

}

以下是一些Storyboard截图: The Table View

The Outlets Inspector

The Attributes Inspector

The Storyboard hierarchy

1 个答案:

答案 0 :(得分:4)

好问题。从我所看到的实现方法是创建一个你出列的自定义xib文件。以下是objective-c中关于该主题的最佳文章:https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722

基本上,您执行以下操作:

  1. 设置您的表就像减去可重复使用的单元格一样
  2. 创建一个自定义xib文件并使其看起来像您的原型单元格(您可以通过制作一个空对象并将一个tableviewcell拖到故事板上来完成此操作
  3. enter image description here

    1. 确保在Identity Inspector
    2. 中将xib的类类型设置为ArtistCell

      enter image description here

      1. 创建一个自定义类ArtistCell以将xib挂钩到
      2. enter image description here

        1. 在设置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!
          }
          
        2. 这是一个屏幕上限,以防万一:

          enter image description here

          此代码已经过全面测试,适合我。仅供参考。 :-)希望这有帮助!

          干杯!
          Rob Norback