我试图执行一个segue并传递被轻拍的音频曲目'到我的下一个viewController。我遇到的问题是,在我的segue中,' indexPath'为null(不从tableView继承tapped值)。有人能指出我正确的方向吗?
// Function for when cell is tapped in tableView
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var sound = self.sounds[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
var pathComponents = [baseString, sound.url]
var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)!
tableView.deselectRowAtIndexPath(indexPath, animated: true)
self.performSegueWithIdentifier("tableViewToPlayerSegway", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "tableViewToPlayerSegway" {
var secondScene = segue.destinationViewController as PlayViewController
if var indexPath = self.tableView.indexPathForSelectedRow() {
var sound = self.sounds[indexPath.row]
var soundName = sound.name
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
var pathComponents = [baseString, sound.url]
var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)!
secondScene.currentSound = audioNSURL
secondScene.soundName = sound.name
}
}
}
答案 0 :(得分:0)
您可以在performSegueWithIdentifier方法中将所选单元格的indexPath作为发送方对象发送,因为您根本不使用它。然后在prepareForSegue中解包并将发送者强制转换为NSIndexPath并使用它将数据传递给下一个控制器
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var sound = self.sounds[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
var pathComponents = [baseString, sound.url]
var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)!
tableView.deselectRowAtIndexPath(indexPath, animated: true)
self.performSegueWithIdentifier("tableViewToPlayerSegway", sender: indexPath)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "tableViewToPlayerSegway" {
var secondScene = segue.destinationViewController as PlayViewController
if var indexPath = sender as? NSIndexPath {
var sound = self.sounds[indexPath.row]
var soundName = sound.name
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
var pathComponents = [baseString, sound.url]
var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)!
secondScene.currentSound = audioNSURL
secondScene.soundName = sound.name
}
}
}