UITableViewCell 重复显示 "Label"
UITableViewCell showing "Label" repeatedly
在 UITableViewCell
中,必须出现的歌曲的名称与情节提要中的 "Label" 一起显示。控制器代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("AudioCell") as AudioCell
var mp3file: AnyObject = mp3sPaths[indexPath.row]
cell.textLabel?.text = mp3file.lastPathComponent
cell.playIcon.text = "▶️"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var mp3file = mp3sPaths[indexPath.row] as NSString
var mp3URL: NSURL = NSURL.fileURLWithPath(mp3file)!
var error: NSError?
audioPlayer?.stop()
audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
audioPlayer?.play()
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AudioCell {
cell.playIcon.text = "◾️"
}
}
和AudioCell的代码class:
class AudioCell: UITableViewCell {
@IBOutlet weak var playIcon: UILabel!
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
应用程序的屏幕截图是there:
那是因为在 tableView 函数中您正在设置单元格文本标签的名称
cell.textLabel?.text = mp3file.lastPathComponent
并且在您的单元格中有标题标签
@IBOutlet weak var titleLabel: UILabel!
您需要做的就是在您的 tableView 函数中
cell.titleLabel?.text = mp3file.lastPathComponent
并确保故事板表格视图单元格中不存在 textLabel
在 UITableViewCell
中,必须出现的歌曲的名称与情节提要中的 "Label" 一起显示。控制器代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("AudioCell") as AudioCell
var mp3file: AnyObject = mp3sPaths[indexPath.row]
cell.textLabel?.text = mp3file.lastPathComponent
cell.playIcon.text = "▶️"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var mp3file = mp3sPaths[indexPath.row] as NSString
var mp3URL: NSURL = NSURL.fileURLWithPath(mp3file)!
var error: NSError?
audioPlayer?.stop()
audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
audioPlayer?.play()
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AudioCell {
cell.playIcon.text = "◾️"
}
}
和AudioCell的代码class: class AudioCell: UITableViewCell {
@IBOutlet weak var playIcon: UILabel!
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
应用程序的屏幕截图是there:
那是因为在 tableView 函数中您正在设置单元格文本标签的名称
cell.textLabel?.text = mp3file.lastPathComponent
并且在您的单元格中有标题标签
@IBOutlet weak var titleLabel: UILabel!
您需要做的就是在您的 tableView 函数中
cell.titleLabel?.text = mp3file.lastPathComponent
并确保故事板表格视图单元格中不存在 textLabel