我正在尝试将自定义UITableViewCell添加到我的Split View Controller Master。 当我运行我的应用程序时,单元格不会出现,尽管它们确实包含println语句确认的信息。 经过一些研究后我发现只生成了标准的UITableViewCells。
Table ViewController具有通过Storyboard连接的数据源和代理,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! }以下是一些故事板截图:
I am trying to add a custom UITableViewCell to my Split View Controller Master. When I run my app the cells don't appear, although they do carry the information as confirmed by a println statement. After some research I found out that only standard UITableViewCells are generated.
The Table ViewController has the Data Source and Delegate connected via the Storyboard and the UITableViewCell has a custom Class with Identifier.
The TableView & TableViewCell are in the same ViewController.
The TableViewCell reusable identifier is "ArtistCell" and it's class is 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 }The custom cell:
class ArtistCell: UITableViewCell { @IBOutlet weak var artistImage: UIImageView! @IBOutlet weak var artistNameLabel: UILabel! @IBOutlet weak var albumsLabel: UILabel! @IBOutlet weak var songsLabel: UILabel! }Here are some Storyboard screenshots:
最满意答案
好问题。 从我所看到的实现方法是创建一个你出列的自定义xib文件。 以下是objective-c中关于该主题的最佳文章: https : //medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722
基本上,您执行以下操作:
设置您的表就像您减去可重复使用的单元格一样 创建一个自定义xib文件并使其看起来像您的原型单元格(您可以通过创建一个空对象并将一个tableviewcell拖到故事板上来完成此操作确保在Identity Inspector中将xib的类类型设置为ArtistCell
创建一个自定义类ArtistCell来挂钩xib
在设置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! }这是一个屏幕上限,以防万一:
此代码已经过全面测试,适合我。 仅供参考。 :-) 希望这可以帮助!
干杯! 罗布诺贝克
Great question. From what I've seen the way to implement this is to create a custom xib file that you dequeue. Here is the best article on the subject in objective-c: https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722
Essentially, you do the following:
Setup your table just like you have minus the reusable cell Create a custom xib file and make it look like your prototype cell (you can simply do this by making an empty object and dragging a tableviewcell onto the storyboard Make sure to set the class type of the xib to ArtistCell in the Identity Inspector Create a custom class ArtistCell to hook the xib up toUse this code when you are setting up your 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! }Here's a screen cap just in case:
This code has been fully tested and works for me. FYI. :-) Hope this helps!
Cheers! Rob Norback
更多推荐
发布评论