拆分视图控制器

编程入门 行业动态 更新时间:2024-10-28 10:21:38
拆分视图控制器 - 自定义UITableViewCell(Split View Controller - Custom UITableViewCell)

我正在尝试将自定义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

enter image description here

Make sure to set the class type of the xib to ArtistCell in the Identity Inspector

enter image description here

Create a custom class ArtistCell to hook the xib up to

enter image description here

Use 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:

enter image description here

This code has been fully tested and works for me. FYI. :-) Hope this helps!

Cheers! Rob Norback

更多推荐

本文发布于:2023-07-15 11:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   控制器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!