Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如果在TableView didSelectRowAtIndexPath函数中选择了其他行,如何将图像更改为以前的状态?_Swift_Uitableview_Playback_Didselectrowatindexpath - Fatal编程技术网

Swift 如果在TableView didSelectRowAtIndexPath函数中选择了其他行,如何将图像更改为以前的状态?

Swift 如果在TableView didSelectRowAtIndexPath函数中选择了其他行,如何将图像更改为以前的状态?,swift,uitableview,playback,didselectrowatindexpath,Swift,Uitableview,Playback,Didselectrowatindexpath,所以我有动态的tableview,每一行都有播放图像。如果我选择行图像将更改为暂停图标。但是,如果我选择了另一行,我需要先前选择的行上的图像具有播放图标。 如何处理这样的功能 我所拥有的只是: class ViewController: UIViewController { var stations = [ (stationIcon: "rr_icon", stationName: "Radio1", urlString: "http://.._320"

所以我有动态的tableview,每一行都有播放图像。如果我选择行图像将更改为暂停图标。但是,如果我选择了另一行,我需要先前选择的行上的图像具有播放图标。 如何处理这样的功能

我所拥有的只是:

     class ViewController: UIViewController {
        var stations = [
        (stationIcon: "rr_icon", stationName: "Radio1", urlString: "http://.._320", isPlaying: false),
        (stationIcon: "mm_icon", stationName: "Record2", urlString: "http://../_320", isPlaying: false),
........]

         func playSelectedStation(indexPath: NSIndexPath) {
        let url = NSURL(string: stations[indexPath.row].urlString)!
        stations[indexPath.row].isPlaying = true
        avPlayer = AVPlayer(URL: url)
        avPlayer.play()
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! RadioTableViewCell
            cell.playPause.image = stations[indexPath.row].isPlaying ? UIImage(named: "cellPause") : UIImage(named: "cellPlay")
            return cell
        }

        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

            switch indexPath.row {
            playSelectedStation(indexPath)
        tableView.reloadData()

无线电台的更改没有问题,只有播放/暂停图标状态有问题

您可以通过查看可见单元格并禁用所有单元格中的播放按钮来实现,用户刚刚点击的除外:

class PlayItem {
    // your code
    var isPlaying = false
}

class RadioTableViewCell: UITableViewCell {

    // your code ....

    func setup(item item: PlayItem) {
        image = item.isPlaying ? UIImage(named: "cellPause") : UIImage(named: "cellPlay")
    }
}
在您的代表中:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! RadioTableViewCell
    let item = items[indexPath.row] // you may think of storing this array in some kind of view model
    cell.setup(item: item)
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as! RadioTableViewCell
    let selectedItem = items[indexPath.row]
    selectedItem.isPlaying = !selectedItem.isPlaying

    for cell in tableView.visibleCells {
        guard let visibleCell = cell as? RadioTableViewCell else { return }
        let path = tableView.indexPathForCell(visibleCell)
        let item = items[path.row]
        item.isPlaying = visibleCell == selectedCell
        visibleCell.setup(item: item)
    }
}

更新:我已更新我的答案以将播放状态存储在项中。

使用MVC模式,而不是将数据存储在视图对象或全局(视图控制器)范围中。那你的生活就容易多了

我认为每一行都应该有一个模型对象作为后盾。说:

class PlayItem {
    ....
    var isPlaying = false
}
然后您的
cellforrowatinexpath
方法:

let playItem = self.playItemArray[indexPath.row]
cell.imageView.image = playItem.isPlaying ? UIImage("cellPause") : UIImage("cellPlay")

然后在
didselectRowatinedexpath
方法中,更改所选项目
isplay=true
和所有其他项目
isplay=false
,并调用
tableView.reloadData()
。“表格视图”将刷新所有单元格以反映其正确状态。

尝试了您的方法,当我点击“播放”时,所有单元格显示带暂停图标的图像。我已编辑了我的答案,但有一个输入错误。你能再试一次吗?太好了!非常感谢。如果我再次点击同一个手机,我需要它停止播放,并将图标更改为播放,这是怎么回事?另一个错误如果我滚动表格视图,一些单元格有暂停状态图标,则必须将状态保留在数据对象中,而不是单元格中。我提供的代码只是展示了如何只设置一个单元格。同样的问题,所有单元格都显示带有暂停图标的图像。什么是让playItem=。。应该是?您有一个数组,其中包含
PlayItem
对象作为表视图的模型。然后
让playItem=self.playItemArray[indexath.row]
您建议将另一个密钥对添加到我现有的数组字典中吗?是的,在每个模型(数组中的字典)中跟踪
isplay
,而不是在视图控制器中。然后仅更改被点击的行的属性。表格单元格是重复使用的,因此当您滚动时,新出现的单元格将是以前消失的单元格,您需要重新配置其所有可视属性。