gpt4 book ai didi

swift - 如何在表格 View 单元格中添加一个按钮,该按钮在每行上快速播放不同的声音文件

转载 作者:可可西里 更新时间:2023-11-01 02:18:55 24 4
gpt4 key购买 nike

我希望能够在 UITableViewCell 中放置一个按钮,我可以在其中单击该按钮,它会在每一行播放不同的声音文件。实现这一目标的最佳代码是什么?

这是我的代码:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var mytableview: UITableView!
var arrayOfPersons: [Person] = [Person]()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

self.setUpPersons()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func setUpPersons()
{
let person1 = Person(name: "Anna\nAnna2\nAnna3", number: 60, imageName: "testingimage.jpg", soundButton: "dimsumgirl.mp3")
let person2 = Person(name: "Joes", number: 10, imageName: "testingimage2.jpg", soundButton: "dimsumgirl.mp3")

arrayOfPersons.append(person1)
arrayOfPersons.append(person2)

}

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

}

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

let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell



if indexPath.row % 2 == 0
{
cell.backgroundColor = UIColor.blackColor()
}
else
{
cell.backgroundColor = UIColor.blackColor()
}

let person = arrayOfPersons [indexPath.row]

cell.setCell(person.name, rightlabelInt: person.number, imageName: person.imageName, soundButton: person.soundButton)



return cell

我试过使用上面的代码,但我似乎不太明白如何将图片变成一个按钮并能够用每个按钮播放声音。

非常感谢您的帮助!

谢谢!

最佳答案

在您的自定义单元格中,您应该有一个按钮并在该单元格内连接触摸事件。在 setCell 内部,将按钮的背景图像设为您传递的图像,并将类变量设置为您传入的声音文件。然后,当触发 IBAction 触摸事件时,播放来自类变量的声音。

关于swift - 如何在表格 View 单元格中添加一个按钮,该按钮在每行上快速播放不同的声音文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32888093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com