我想添加一个自定义的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!
}
您是否将单元格设计为同一个故事板文件中的表格视图的原型单元格? – hennes
是的,细胞是一个原型,并在同一个故事板。 –
它们是否也在同一个ViewController中? –