UITableViewCell 运行结束后图片变化
UITableViewCell Image change after operation is finished
音频结束后更改图像时遇到问题。我如何更改单元格的图像?请帮我。我用 'audioPlayerDidFinishPlaying' 打印了 "Done" 但我不知道如何控制单元格图像等
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if messageList[indexPath.row].fileAudioUid != nil {
let url = "..."
startDownload(audioUrl: url)
}
}
var audioPlayer : AVAudioPlayer!
func startDownload(audioUrl:String) -> Void {
let fileUrl = self.getSaveFileUrl(fileName: audioUrl)
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
return (fileUrl, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(audioUrl, to:destination)
.downloadProgress { (progress) in
print(progress)
}
.responseData { (data) in
let url1 = data.destinationURL?.absoluteURL
print(url1!)
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: url1!)
self.audioPlayer.delegate = self
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}catch let error {
print(error)
}
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
print("Done")
} else {
// did not finish successfully
}
}
保留对单元格的引用
let cellRef: IndexPath?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if messageList[indexPath.row].fileAudioUid != nil {
let url = "..."
startDownload(audioUrl: url)
cellRef = indexPath
}
}
然后
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
print("Done")
let cell = tableView.cellForRow(at:cellRef)
cell.image = "myImage.png"
} else {
// did not finish successfully
}
}
我看到你的音频下载只有一个方法,如果你一个接一个地点击多个单元格,所有的单元格都在你的视图控制器中调用相同的方法来下载音频。您需要为要为其下载音频的单元格提供参考。所以我说在单元格 class 内实现音频下载方法,下载成功后,更新单元格的数据源,音频已经下载,就像你所做的那样
if messageList[indexPath.row].fileAudioUid != nil {
let url = "..."
startDownload(audioUrl: url)
cellRef = indexPath
}
很高兴你有一个音频播放器实例。您需要为 cell 实现自己的协议方法,该方法在完成下载后调用委托方法为 cell
播放音频
协议实施示例可在以下位置获得:
这是针对 collectionview 单元格的,但它是同一件事,您也可以针对 tableview 单元格执行此操作,
现在您可以在单元格中播放音频..并且还维护正在播放其音频的单元格的引用,正如@stevenpcurtis 所回答的那样,并且在成功完成音频播放后,获取该索引路径的单元格并更新图像视图的图像相应。
不是在 AVAudioPlayer 委托中使用单元格,而是使用布尔状态来知道音频是否已完成。
或者使用 AVAudioPlayer 的 isPlaying 属性 来知道它是否正在播放,您可以相应地在 "CellForRow" 方法中更改图像。
更新 UItableViewCell
图片的简单方法。
跟踪 IndexPath
。
var cellRef: IndexPath?
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
print("Done")
guard let cell = tableView.cellForRow(at: cellRef) as? YourTableViewCell else {
return
}
cell.imageView?.image = UIImage(named: "YourImageName")
self.tableView.beginUpdates()
self.tableView.endUpdates()
} else {
// did not finish successfully
}
}
音频结束后更改图像时遇到问题。我如何更改单元格的图像?请帮我。我用 'audioPlayerDidFinishPlaying' 打印了 "Done" 但我不知道如何控制单元格图像等
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if messageList[indexPath.row].fileAudioUid != nil {
let url = "..."
startDownload(audioUrl: url)
}
}
var audioPlayer : AVAudioPlayer!
func startDownload(audioUrl:String) -> Void {
let fileUrl = self.getSaveFileUrl(fileName: audioUrl)
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
return (fileUrl, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(audioUrl, to:destination)
.downloadProgress { (progress) in
print(progress)
}
.responseData { (data) in
let url1 = data.destinationURL?.absoluteURL
print(url1!)
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: url1!)
self.audioPlayer.delegate = self
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}catch let error {
print(error)
}
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
print("Done")
} else {
// did not finish successfully
}
}
保留对单元格的引用
let cellRef: IndexPath?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if messageList[indexPath.row].fileAudioUid != nil {
let url = "..."
startDownload(audioUrl: url)
cellRef = indexPath
}
}
然后
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
print("Done")
let cell = tableView.cellForRow(at:cellRef)
cell.image = "myImage.png"
} else {
// did not finish successfully
}
}
我看到你的音频下载只有一个方法,如果你一个接一个地点击多个单元格,所有的单元格都在你的视图控制器中调用相同的方法来下载音频。您需要为要为其下载音频的单元格提供参考。所以我说在单元格 class 内实现音频下载方法,下载成功后,更新单元格的数据源,音频已经下载,就像你所做的那样
if messageList[indexPath.row].fileAudioUid != nil {
let url = "..."
startDownload(audioUrl: url)
cellRef = indexPath
}
很高兴你有一个音频播放器实例。您需要为 cell 实现自己的协议方法,该方法在完成下载后调用委托方法为 cell
播放音频协议实施示例可在以下位置获得:
这是针对 collectionview 单元格的,但它是同一件事,您也可以针对 tableview 单元格执行此操作,
现在您可以在单元格中播放音频..并且还维护正在播放其音频的单元格的引用,正如@stevenpcurtis 所回答的那样,并且在成功完成音频播放后,获取该索引路径的单元格并更新图像视图的图像相应。
不是在 AVAudioPlayer 委托中使用单元格,而是使用布尔状态来知道音频是否已完成。 或者使用 AVAudioPlayer 的 isPlaying 属性 来知道它是否正在播放,您可以相应地在 "CellForRow" 方法中更改图像。
更新 UItableViewCell
图片的简单方法。
跟踪 IndexPath
。
var cellRef: IndexPath?
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
print("Done")
guard let cell = tableView.cellForRow(at: cellRef) as? YourTableViewCell else {
return
}
cell.imageView?.image = UIImage(named: "YourImageName")
self.tableView.beginUpdates()
self.tableView.endUpdates()
} else {
// did not finish successfully
}
}