我正在尝试从 swift 字典中获取一个值以显示为单元格文本。我收到错误:
类型“任何”没有下标成员
cell.audioLabel.text = audiofiles["filetitle"] <-line producing error
我相信我可能错误地设置了变量,使用 didSelectRowAt 从另一个表视图中使用 segue 传递值。
var audios = Array<Any>() <- gets from another controller
这是我当前的视图控制器代码:
import UIKit
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var audioTable: UITableView!
@IBOutlet weak var descText: UITextView!
@IBOutlet weak var clickButton: UIButton!
var practitle = String()
var desc = String()
var hasAudio = Int()
var audios = Array<Any>()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = practitle
descText.text = desc
if hasAudio != 0 {
clickButton.isHidden = false
}
print(audios)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return audios.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "audiocell", for: indexPath) as! DetailViewCell
let audiofiles = audios[indexPath.row]
cell.audioLabel.text = audiofiles["filetitle"]
return cell
}
当我使用
print(audios)
我得到的结果是:
[demoApp.Audiofile(id: 1, filetitle: "样本文件一", filename: "breath5mins", fileformat: "mp3"), demoApp.Audiofile(id: 2, filetitle: "样本文件二", filename: "breath10mins ", fileformat: "mp3"), demoApp.Audiofile(id: 3, filetitle: "示例文件三", filename: "breath20mins", fileformat: "mp3")]
如何使用文件标题作为单元格的标签文本?
目标是显示标题并在单击单元格时打开另一个视图,并允许用户单击新视图上的按钮并播放 mp3 文件。