ios - 为什么程序无法识别第二次按钮点击?

标签 ios swift

由于某种原因,程序无法识别第二次按钮单击,它会识别执行操作的第一次按钮,但是当我单击另一个单元格中的另一个按钮时,它无法识别该操作。下面是添加到每个单元格的按钮的代码以及按钮功能。当单击 x 单元格的按钮时,它将采用该标题并通过 itunes 搜索 api 运行它并播放预览链接。当我单击 x 单元格的按钮时它起作用,但是当我单击另一个单元格中的另一个按钮时没有任何反应。为什么?我该如何解决这个问题?

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")




    cell.textLabel?.text = ret[indexPath.row]
    cell.detailTextLabel?.text = ter[indexPath.row]
    cell.textLabel?.font = UIFont(name: "Lombok", size: 22)
    cell.textLabel?.textColor =  UIColorFromRGB("4A90E2")
    cell.detailTextLabel?.font = UIFont(name: "Lombok", size: 16)
    cell.detailTextLabel?.textColor =  UIColor.blackColor()

    let playButton : UIButton = UIButton(type: UIButtonType.Custom)
    playButton.tag = indexPath.row
    let imageret = "playbutton"
    playButton.setImage(UIImage(named: imageret), forState: .Normal)
    playButton.frame = CGRectMake(230, 20, 100, 100)
    playButton.addTarget(self,action: "playit:", forControlEvents: UIControlEvents.TouchUpInside)
    for view: UIView in cell.contentView.subviews {
        view.removeFromSuperview()
    }


    cell.contentView.addSubview(playButton)


    cell.textLabel?.numberOfLines = 0;
    cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

    cell.backgroundColor = UIColor.clearColor()






    return cell
}





func playit(sender: UIButton!) {

    let playButtonrow = sender.tag

    print(ret[playButtonrow])

    let searchTerm: String = ret[playButtonrow]

    let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)

   if let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
    let urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music"
    let url: NSURL = NSURL(string: urlPath)!
    let request: NSURLRequest = NSURLRequest(URL: url)
    let ctn: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!


    print("Search iTunes API at URL \(url)")

    ctn.start()

    let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) -> Void in
        do {
            if let dict: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
            {


                let previewUrl = NSURL(string: (dict["results"]![0]["previewUrl"] as? String)!)!
                        print(previewUrl)
                player = AVPlayer(URL: previewUrl)
                player.rate = 1.0
                player.play()



            }
        } catch let jsonError as NSError {

        }
    }
    task.resume()



    }

}

最佳答案

playit: 根本没有调用?

尝试在您的cellForRowAtIndexPath 方法中使用正确的table.dequeueReusableCellWithIdentifier,而不是playit

尝试删除 let cell = table.dequeueReusableCellWithIdentifier("cell")

最好不要删除 subview 并再次添加,重置目标方法。

关于ios - 为什么程序无法识别第二次按钮点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33600573/

相关文章:

objective-c - react native 桥接 : Passing JSON to Swift function

ios - 如何仅在中国推出我的 iOS iPhone 应用程序?

ios - 为什么括号会在 objective-c 中搞乱这个数学?

ios - 更新播放器远程控制事件的目标操作方法(MPRemoteCommand)

objective-c - 绿屏/色度键 iOS

ios - 按频率列出数组中的对象,最常出现在最前面

ios - 在 Swift 中比较 2 个字符串失败

ios - 如何在另一个文件中定义的函数中获取 VC 的引用

ios - UICollectionView 的框架未正确更新为 UITableViewCell subview

ios - 指定 API 请求的参数