Swift 在UITableView中使用CustomStringConvertible
我声明如下:Swift 在UITableView中使用CustomStringConvertible,swift,uitableview,customstringconvertible,Swift,Uitableview,Customstringconvertible,我声明如下: class Song: CustomStringConvertible { let title: String let artist: String init(title: String, artist: String) { self.title = title self.artist = artist } var description: String { return "\(title)
class Song: CustomStringConvertible {
let title: String
let artist: String
init(title: String, artist: String) {
self.title = title
self.artist = artist
}
var description: String {
return "\(title) \(artist)"
}
}
var songs = [
Song(title: "Song Title 3", artist: "Song Author 3"),
Song(title: "Song Title 2", artist: "Song Author 2"),
Song(title: "Song Title 1", artist: "Song Author 1")
]
我想将此信息输入到UITableView
,特别是表格视图:cellforrowatinexpath:
例如:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell : LibrarySongTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Library Cell") as! LibrarySongTableViewCell
cell.titleLabel = //the song title from the CustomStringConvertible[indexPath.row]
cell.artistLabel = //the author title from the CustomStringConvertible[indexPath.row]
}
我该怎么做?我想不出来
非常感谢 我想你可能把定制和其他设计模式混为一谈了。首先,答案是:
// You have some container class with your tableView methods
class YourTableViewControllerClass: UIViewController {
// You should probably maintain your songs array in here, making it global is a little risky
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell : LibrarySongTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Library Cell") as! LibrarySongTableViewCell
// Get the song at the row
let cellSong = songs[indexPath.row]
// Use the song
cell.titleLabel.text = cellSong.title
cell.artistLabel.text = cellSong.artist
}
}
因为单元格的标题/艺术家已经是公共字符串,所以您可以根据需要使用它们。CustomStringConvertible将允许您将实际对象本身用作字符串。因此,在您的例子中,您可以拥有一首歌曲
,然后调用歌曲.description
,它将打印出“title artist”。但是如果你想使用歌曲的标题
和艺术家
,你只需要调用song.title
和song.artist
另外,正如我上面所写的,尝试将
歌曲
数组移动到ViewController中。也可以考虑使用<代码>结构> <代码> s而不是<代码>类< /代码> s,用于您的<代码>歌曲< /代码>类型。 我认为您可能与其他一些设计模式相混淆。首先,答案是:
// You have some container class with your tableView methods
class YourTableViewControllerClass: UIViewController {
// You should probably maintain your songs array in here, making it global is a little risky
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell : LibrarySongTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Library Cell") as! LibrarySongTableViewCell
// Get the song at the row
let cellSong = songs[indexPath.row]
// Use the song
cell.titleLabel.text = cellSong.title
cell.artistLabel.text = cellSong.artist
}
}
因为单元格的标题/艺术家已经是公共字符串,所以您可以根据需要使用它们。CustomStringConvertible将允许您将实际对象本身用作字符串。因此,在您的例子中,您可以拥有一首歌曲
,然后调用歌曲.description
,它将打印出“title artist”。但是如果你想使用歌曲的标题
和艺术家
,你只需要调用song.title
和song.artist
另外,正如我上面所写的,尝试将
歌曲
数组移动到ViewController中。也可以考虑使用<代码>结构> <代码> s,而不是<代码>类< /代码> s,用于<代码>歌曲< /C> >类型。 首先,您的控制器必须实现UITababVIEW DATABASECE。
那么
首先,控制器必须实现UITableViewDataSource。 那么