由于某种原因,程序无法识别第二次按钮单击,它会识别执行操作的第一次按钮,但是当我单击另一个单元格中的另一个按钮时,它无法识别该操作。下面是添加到每个单元格的按钮的代码以及按钮功能。当单击 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/