为什么自定义单元格按钮图像没有更新为数组值?

时间:2018-12-21 06:04:55

标签: swift

基本上,我有我保存的音乐数组和此下载的url,如果不等于任何值,则我希望该图像为选中标记,因此它表示用户已下载该图像,否则为下载按钮。

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    if savedMusicArray[arrayindex!.row].downloadedUrl != "" {
        self.downloadButon.setImage(UIImage(named: "check"), for: .normal)
    } else {
        self.downloadButon.setImage(UIImage(named: "download2"), for: .normal)
    }

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

导入UIKit

类DownloadPopup:UIViewController {

@IBOutlet var mainLabel: UILabel!
@IBOutlet var songLabel: UILabel!
@IBOutlet var startButton: UIButton!
@IBOutlet var popupView: UIView!
@IBOutlet var cancelButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    setupView()
    songLabel.text = songToDownload.name!
}
func setupView() {
    popupView.addRoundedCorners(radius: 10)
    startButton.layer.cornerRadius = startButton.frame.height / 2
    startButton.addBorder(color: .white, width: 1)
    cancelButton.layer.cornerRadius = startButton.frame.height / 2
    cancelButton.addBorder(color: .white, width: 1)
    cancelButton.addTarget(self, action: #selector(handleCancel), for: .touchUpInside)
    startButton.addTarget(self, action: #selector(handleDownload), for: .touchUpInside)
    popupView.addBorder(color: .white, width: 1)
}
@objc func handleCancel() {
    dismiss(animated: true, completion: nil)
}
@objc func handleDownload() {
    let downloader = Downloader(self)
    let url = URL(string: songToDownload.url!)
    downloader.download(url: url!, index: arrayindex!)
}

}

1 个答案:

答案 0 :(得分:0)

您必须在handleDownload函数或cellForRowAt中编写代码按钮图像更改代码。并在handleDownload函数中,如果要获取特定的单元格,请这样编写。

//由此,您可以获得特定的音乐,这是单元格上的列表

let music = savedMusicArray[sender.tag]

//从这里您可以得到特定的行来更改按钮的图像

let indexPath = IndexPath(row: sender.tag, section: 0)
let cell= self.tableView.cellForRow(at: indexPath) as! MusicLibraryCell

然后,您可以更改特定行的按钮图像,然后重新加载行或重新加载tableview。 否则,您可以在cellForRowAt中编写按钮更改图像代码,但是必须从按钮操作或完成下载图像后重新加载表视图。