我正在尝试将自定义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
}
答案 0 :(得分:4)
好问题。从我所看到的实现方法是创建一个你出列的自定义xib文件。以下是objective-c中关于该主题的最佳文章:https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722
基本上,您执行以下操作:
在设置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!
}
这是一个屏幕上限,以防万一:
此代码已经过全面测试,适合我。仅供参考。 :-)希望这有帮助!
干杯!
Rob Norback