我已經聲明使用CustomStringConvertible如下:在UITableView的
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
,特別是在tableView:cellForRowAtIndexPath:
。
像這樣的:
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]
}
我會怎麼做呢?我無法弄清楚。
非常感謝!