gpt4 book ai didi

ios - 从 UITableViewCell 子类调用 UIActionSheet

转载 作者:行者123 更新时间:2023-11-28 07:02:10 25 4
gpt4 key购买 nike

我正在制作一个表单,其中有一堆不同的 UITableViewCells,具有不同的功能,所以我不想让我的 UITableView 类因每个 Action 而乱七八糟,我想在单元格子类本身上实现每个功能。

所以,我现在有一个单元格,其中会弹出一个 UIActionSheet,但是当我点击时没有任何反应,即使已经设置了委托(delegate)。

我的想法是错误的吗?换一种方式会更好吗?这是一些简单的代码。

import UIKit
class STypeCell: UITableViewCell, UIActionSheetDelegate {

func chooseType() {
var sheet: UIActionSheet = UIActionSheet()
let title: String = "Please choose a course"
sheet.delegate = self
sheet.title = title
sheet.addButtonWithTitle("Cancel")
sheet.addButtonWithTitle("A course")
sheet.addButtonWithTitle("B course")
sheet.addButtonWithTitle("C course")
sheet.cancelButtonIndex = 0
sheet.showInView(self.superview)
}

func actionSheet(sheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
println("this never gets called")
}

func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
println("this never gets called")
}

func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
println("this never gets called")
}

最佳答案

MVC Patterned framework as iOS is one 中,TableViewCell 不应该有那种逻辑。 .

如果您想重用该逻辑,那么您应该创建一个 TableViewController(或 ViewController),然后将其子类化。在 TableView 委托(delegate)方法中,您将调用您的操作。

例子:

class MySuperTableView: UITableViewController, UIActionSheetDelegate {

// [...]

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
self.chooseType()
}

func chooseType() {
var sheet: UIActionSheet = UIActionSheet()
let title: String = "Please choose a course"
sheet.delegate = self
sheet.title = title
sheet.addButtonWithTitle("Cancel")
sheet.addButtonWithTitle("A course")
sheet.addButtonWithTitle("B course")
sheet.addButtonWithTitle("C course")
sheet.cancelButtonIndex = 0
sheet.showInView(self.view)
}

func actionSheet(sheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
println("this never gets called")
}

func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
println("this never gets called")
}

func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
println("this never gets called")
}

// [...]
}

class MyChildTableView: MySuperTableView {

// [...]

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
super.tableView(tableView, didSelectRowAtIndexPath: indexPath)
}

// [...]

}

class MyOtherChildTableView: MySuperTableView {

// [...]

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
super.tableView(tableView, didSelectRowAtIndexPath: indexPath)
}

// [...]

}

当然,更多的逻辑会有所帮助,比如:点击了什么样的行?我是否有其他可以点击的 TableViewCells,因此我应该检查一下?

关于ios - 从 UITableViewCell 子类调用 UIActionSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769549/

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