UITableViewCell重复显示“标签”

时间:2015-02-03 15:35:20

标签: ios uitableview swift

UITableViewCell中,必须出现的歌曲名称显示为"标签"从故事板。控制器代码:

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

1 个答案:

答案 0 :(得分:0)

这是因为在tableView函数中你设置了单元格textLabel的名称

cell.textLabel?.text = mp3file.lastPathComponent

并在您的单元格中有标题标签

@IBOutlet weak var titleLabel: UILabel!

您需要做的只是在tableView函数

cell.titleLabel?.text = mp3file.lastPathComponent

并确保故事板tableview单元格中不存在textLabel