Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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
Ios 如何根据点击的UITableView行更改方法的参数?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何根据点击的UITableView行更改方法的参数?

Ios 如何根据点击的UITableView行更改方法的参数?,ios,swift,uitableview,Ios,Swift,Uitableview,我是一个新的程序员,在实现它时遇到了一些困难。我在数据模型中设置了几个类,如下所示: 我在这里创建我的对象: import Foundation import UIKit class PracticePads { var padTitle: String var padImage: UIImage var soundPreset: Int init(pad: UIImage, title: String, preset: Int ) { self.padImage = pa

我是一个新的程序员,在实现它时遇到了一些困难。我在数据模型中设置了几个类,如下所示:

我在这里创建我的对象:

import Foundation
import UIKit

class PracticePads {

var padTitle: String

var padImage: UIImage

var  soundPreset: Int

init(pad: UIImage, title: String, preset: Int ) {

    self.padImage = pad
    self.padTitle = title
    self.soundPreset = preset
}

}
然后是另一个我创建如下数组的文件:

class PracticePadBank {

var list = [PracticePads]()

init(){

        let pad1 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 1", preset: 0)
        let pad2 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 2", preset: 0)

        let pad3 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 3", preset: 0)
        let pad4 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 4", preset: 0)
        let pad5 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 5", preset: 0)            
        list.append(pad1)
        list.append(pad2)
        list.append(pad3)
        list.append(pad4)
        list.append(pad5) 
}
}
class ChooseViewController: UIViewController {

var pads: [PracticePads] = []
let allPads = PracticePadBank()
var audioPlayer = AVAudioPlayer()

@IBOutlet weak var tableView: UITableView!


  override func viewDidLoad() {
  super.viewDidLoad()
    pads = allPads.list

}

}

extension ChooseViewController: UITableViewDataSource, UITableViewDelegate{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pads.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let pad = pads[indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "PadCell") as! PadCell

    cell.setPad(pad: pad)

    return cell

}




func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self .performSegue(withIdentifier: "BlackToDetail", sender: nil)

    }  
}
在另一个ViewController中,我设置了一个UITableView,允许用户选择如下“练习板”:

class PracticePadBank {

var list = [PracticePads]()

init(){

        let pad1 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 1", preset: 0)
        let pad2 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 2", preset: 0)

        let pad3 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 3", preset: 0)
        let pad4 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 4", preset: 0)
        let pad5 = PracticePads(pad: #imageLiteral(resourceName: "Hard Neoprene Pad"), title: "Pad 5", preset: 0)            
        list.append(pad1)
        list.append(pad2)
        list.append(pad3)
        list.append(pad4)
        list.append(pad5) 
}
}
class ChooseViewController: UIViewController {

var pads: [PracticePads] = []
let allPads = PracticePadBank()
var audioPlayer = AVAudioPlayer()

@IBOutlet weak var tableView: UITableView!


  override func viewDidLoad() {
  super.viewDidLoad()
    pads = allPads.list

}

}

extension ChooseViewController: UITableViewDataSource, UITableViewDelegate{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pads.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let pad = pads[indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "PadCell") as! PadCell

    cell.setPad(pad: pad)

    return cell

}




func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self .performSegue(withIdentifier: "BlackToDetail", sender: nil)

    }  
}
此UITableView会将用户带到另一个VC,在那里他们可以选择不同的练习。。我现在设置了UIButtons,以转换到另一个VC,其中的方法是,我希望根据点击的行更改(预设)的参数:

func loadSF2PresetIntoSampler(preset:UInt8)  {

    guard let bankURL = Bundle.main.url(forResource: "Pad Sounds", withExtension: "sf2") else {
        print("could not load sound font")
        return
    }

    do {
        try sampler.loadSoundBankInstrument(at: bankURL, program: preset,bankMSB: UInt8(kAUSampler_DefaultMelodicBankMSB),bankLSB: UInt8(kAUSampler_DefaultBankLSB))
    } catch {
        print("error loading sound bank instrument")
    }
}
我不知道该怎么做

在此提前感谢您的帮助


***我还有一个问题-如果有问题的ViewController在堆栈中彼此紧挨着,那么一切都很好,但是我需要通过大约3个其他的ViewController来传递数据。。。我尝试使用下面答案中列出的prepare for Segue方法,但是我想调用该方法的ViewController上的print语句表示单元格为nil。我不确定如何将数据从第一个ViewController(获取单元格的位置)保存到要访问第一个ViewController单元格索引的ViewController。希望这有意义!让我知道我是否应该显示我当前更新的代码来帮助说明这一点。。。谢谢大家!

您需要传递
indexPath.row

 self .performSegue(withIdentifier: "BlackToDetail", sender: indexPath.row)
//

//


你好,谢谢你在这里帮忙。我尝试添加此代码,在目标VC(您正在调用的下一个ViewController-我的名为“ViewController”)中,我收到一个SIGABRT错误,该错误表示:无法将“SampleSequencer.BlackViewController”(0x10a3b2520)类型的值强制转换为“SampleSequencer.ViewController”(0x10a3b2728)。另外,我不知道在你的例子之后如何继续——就像在实际改变价值观(我正计划做一个有趣的事情)谢谢你!您是如何在prepareForSegue中计算值的,nextVC是BlackViewController吗?嗨,我只是有点困惑-可能是为什么我会有这个问题!所以,从ChooseVC,它转到BlackVC,然后转到MainVC,然后我想要访问的方法是在“ViewController”中,我获取了您提供的代码并输入了以下内容:override func prepare(对于segue:UIStoryboardSegue,sender:Any?{if segue.identifier==“BlackToDetail”{let nextScene=segue.destination as!ViewController nextScene.selectedRow=sender as?Int}我猜这是因为segue转到了黑色vc,而我需要的segue是两个vc之外的名为“nextVC”?我将segue标识符更改为正确的名称“nextVC”崩溃停止了。现在我想知道如何尝试更改参数?好的,马上…我必须努力。你是最棒的,@shu Khan!!