在 TableView 中显示来自 UITableViewCell 的 UIAlertView
Show UIAlertView from UITableViewCell in TableView
这里是初学者。很难理解委托以及如何实施它们。
我有一个 TableViewCell,用于确定音频文件是否存在。
如果没有音频文件,则在包含 tableViewCell 的 tableView 中显示一个 alertView。有人告诉我使用委托可以解决这个问题,但不知道如何使用它们。
这是 TableViewCell 的代码,它决定是否显示警报:
class TablePostCellView: UITableViewCell, AVAudioPlayerDelegate {
...
@IBAction func playAudio(sender: AnyObject) {
//self.post?.audioObject.parsePlaySound()
if self.post?.audioObject.parseSoundPlayBack == nil{
println("no audio show alert")
} else{
println("playAudio")
parseSoundPlayBack.play()
}
}
我读了这个: 但它并没有真正帮助我。
很多人会告诉您在 UITableView 和 UITableViewCell 之间实现一个 delegate/protocol 模式。他们的意思通常是这样的:
protocol AudioPlayable {
func playSoundsIfAble()
}
class SomeCell: UITableViewCell {
var delegate: AudioPlayable?
@IBAction func userDidSomething(sender: AnyObject) {
delegate?.playSoundsIfAble()
}
class SomeTableView: UITableViewController {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeReusableCellWithIdentifier("abcd") as SomeCell!
cell.delegate = self
return cell
}
func playSoundsIfAble() {
//Play the sound or show the alert
}
}
但是尽管委托协议模式很常见,为什么不走在曲线的前面,作为 Swift 中的第一个 class 公民使用函数呢!
在某些情况下,这是一种非常有用的替代方法:
class SomeTableView: UITableViewController, UITableViewDataSource {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("abcd") as! SomeClass
cell.playSpecificAudioFile = {
//Grab a specific audio file that corresponds with this specific cell, and play it
//Since this is being written on the TableView, and not the Cell,
//you probably have access to all the data source stuff you need
//and are much better suited to lining up the indexPath and dataSource here
}
return cell
}
}
private typealias PlaySpecificAudioFile = () -> ()
class SomeClass: UITableViewCell {
private var playSpecificAudioFile: PlaySpecificAudioFile?
@IBAction func userDidSomething(sender: AnyObject) {
playSpecificAudioFile?()
}
}
这里是初学者。很难理解委托以及如何实施它们。
我有一个 TableViewCell,用于确定音频文件是否存在。 如果没有音频文件,则在包含 tableViewCell 的 tableView 中显示一个 alertView。有人告诉我使用委托可以解决这个问题,但不知道如何使用它们。
这是 TableViewCell 的代码,它决定是否显示警报:
class TablePostCellView: UITableViewCell, AVAudioPlayerDelegate {
...
@IBAction func playAudio(sender: AnyObject) {
//self.post?.audioObject.parsePlaySound()
if self.post?.audioObject.parseSoundPlayBack == nil{
println("no audio show alert")
} else{
println("playAudio")
parseSoundPlayBack.play()
}
}
我读了这个:
很多人会告诉您在 UITableView 和 UITableViewCell 之间实现一个 delegate/protocol 模式。他们的意思通常是这样的:
protocol AudioPlayable {
func playSoundsIfAble()
}
class SomeCell: UITableViewCell {
var delegate: AudioPlayable?
@IBAction func userDidSomething(sender: AnyObject) {
delegate?.playSoundsIfAble()
}
class SomeTableView: UITableViewController {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeReusableCellWithIdentifier("abcd") as SomeCell!
cell.delegate = self
return cell
}
func playSoundsIfAble() {
//Play the sound or show the alert
}
}
但是尽管委托协议模式很常见,为什么不走在曲线的前面,作为 Swift 中的第一个 class 公民使用函数呢!
在某些情况下,这是一种非常有用的替代方法:
class SomeTableView: UITableViewController, UITableViewDataSource {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("abcd") as! SomeClass
cell.playSpecificAudioFile = {
//Grab a specific audio file that corresponds with this specific cell, and play it
//Since this is being written on the TableView, and not the Cell,
//you probably have access to all the data source stuff you need
//and are much better suited to lining up the indexPath and dataSource here
}
return cell
}
}
private typealias PlaySpecificAudioFile = () -> ()
class SomeClass: UITableViewCell {
private var playSpecificAudioFile: PlaySpecificAudioFile?
@IBAction func userDidSomething(sender: AnyObject) {
playSpecificAudioFile?()
}
}