为什么该程序无法识别第二个按钮点击?

时间:2015-11-09 00:07:08

标签: 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()



    }

}

2 个答案:

答案 0 :(得分:0)

playit:根本没有被召唤?

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

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

最好不要删除子视图并重新添加,重置目标方法。

答案 1 :(得分:0)

糟糕的选择: 1)尝试调试 - 查看调试 - 捕获视图层次结构,以查看在层次结构中是否没有比按钮更高的值。

2)在-willDisplayCell上添加按钮代码

更好的选择: 子类UItableViewCell并在子类中添加您的按钮。

(为了上帝的缘故!使用  var cell = tableView.dequeueReusableCellWithIdentifier(" CELL")为?的UITableViewCell)