gpt4 book ai didi

ios - UITableViewCell 中的 UIButton 被重用

转载 作者:行者123 更新时间:2023-11-28 07:00:31 26 4
gpt4 key购买 nike

我的表格 View 显示播客数据。所有单元格都可以很好地重复使用,并以正确的顺序显示播客。我将下载和播放按钮添加为每个单元格的 subview 。当我向下滚动列表时,将显示用于隐藏未下载剧集的播放按钮,并将保存先前启动下载的单元格的数据。例如,如果我点击单元格 1 中的下载按钮,它将下载正确的剧集,播放按钮将完美播放该剧集。如果我向下滚动到单元格 10,将出现相同的播放按钮并从按钮 1 播放剧集。我的代码有什么问题?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cell: PFTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PFTableViewCell
if let title = object?["title"] as? String {
cell.textLabel?.text = title
let downloadButton = UIButton(type: UIButtonType.Custom)
downloadButton.frame = CGRectMake(cell.contentView.bounds.width - 100, cell.contentView.bounds.height / 2, 100, 35)
downloadButton.setTitle("Download", forState: .Normal)
downloadButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
downloadButton.tag = indexPath.row
downloadButton.addTarget(self, action: "downloadEpisode:", forControlEvents: .TouchUpInside)
cell.addSubview(downloadButton)
let playButton = UIButton(type: UIButtonType.Custom)
playButton.frame = CGRectMake(cell.contentView.bounds.width - 100, cell.contentView.bounds.height - 89, 100, 35)
playButton.setTitle("Play", forState: .Normal)
playButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
playButton.tag = indexPath.row
playButton.addTarget(self, action: "playEpisode:", forControlEvents: .TouchUpInside)
if let isDownloaded = object?["isDownloaded"] as? String {
if isDownloaded == "yes" {
playButton.hidden = false
} else {
playButton.hidden = true
}
}
cell.addSubview(playButton)
}
return cell
}

编辑:

我也试过这个但它不起作用并且仍然为每个单元格创建 1 个按钮:

     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cell: PFTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PFTableViewCell
if let title = object?["title"] as? String {
cell.textLabel?.text = title
}
if let button = cell.viewWithTag(indexPath.row) {
print(button)
} else {
if let isDownloaded = object?["isDownloaded"] as? String {
if isDownloaded == "yes" {
let downloadButton = UIButton(type: UIButtonType.Custom)
downloadButton.frame = CGRectMake(cell.contentView.bounds.width - 100, cell.contentView.bounds.height / 2, 100, 35)
downloadButton.setTitle("Play", forState: .Normal)
downloadButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
downloadButton.tag = indexPath.row
downloadButton.addTarget(self, action: "playEpisode:", forControlEvents: .TouchUpInside)
cell.addSubview(downloadButton)
} else {
let downloadButton = UIButton(type: UIButtonType.Custom)
downloadButton.frame = CGRectMake(cell.contentView.bounds.width - 100, cell.contentView.bounds.height / 2, 100, 35)
downloadButton.setTitle("Download", forState: .Normal)
downloadButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
downloadButton.tag = indexPath.row
downloadButton.addTarget(self, action: "downloadEpisode:", forControlEvents: .TouchUpInside)
cell.addSubview(downloadButton)
}
}
}

return cell
}

编辑2:

新增下载方式:

        func downloadEpisode(sender: UIButton) {
print("Downloading..")
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)

let object = self.objectAtIndexPath(indexPath)
if let result = object {
let urlstring = result["downloadURL"] as? String
if urlstring != nil {
let episodeURL = NSURL(string: urlstring!)
downloader.downloadPodcastEpisode(episodeURL!, podcast: result)
}
}
}

回答:

尽管所有答案都是正确的,但我最终还是决定删除手机点击上的下载/播放按钮。非常感谢!

最佳答案

问题是您每次都添加一个 按钮。当您的单元格被重复使用时,它已经有了一个按钮。您需要编写代码来保证按钮只会创建一次,然后修改现有按钮而不是创建新按钮。

有两种常见的方法:

  • 使用 viewWithTag 检查按钮是否存在(创建一个按钮,如果不存在则设置其标签)
  • 子类 UITableViewCell,在初始化程序中创建按钮,并在每次使用单元格时配置按钮。

搜索“uitableviewcell viewwithtag”或“subclass uitableviewcell”应该会为您提供大量示例代码,因此我不会在这里重复。

关于ios - UITableViewCell 中的 UIButton 被重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32165842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com