从 Swift 中的 TableViewCell Class 切换视图控制器
Switch View Controller From TableViewCell Class in Swift
我想在用户单击 tableviewcell 中的元素时显示一个新的 ViewController。但是,用于启动 VC 的标准代码在 tableview 单元格中甚至在助手 class 中都不起作用,因为 TVC 和助手 class 都无法呈现一个视图控制器。
这是帮助程序中的代码 class。无论是放置在 helperclass 还是 tableview 单元格中,它都没有启动 VC 的 present 方法。
class launchVC {
func launchVCNamed(identifier: String) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyBoard.instantiateViewController(withIdentifier: "contactDetail")
//FOLLOWING LINE HAS ERROR NO SUCH MEMBER (present)
self.present(secondVC, animated: true, completion: nil)
}
}
如何修改它以启动 VC?
通常您应该使用委托模式或闭包将块从单元格传回视图控制器。我更喜欢使用闭包而不是委托,所以我会举这样的例子:
class SomeCell: UITableViewCell {
var actionBlock = { }
func someActionOccured() { // some action like button tap in cell occured
actionBlock()
}
}
并且在 cellForRow
中,您需要在视图控制器中分配闭包
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SomeCell // replace cell identifier with whatever your identifier is
cell.actionBlock = { [unowned self] in
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyBoard.instantiateViewController(withIdentifier: "contactDetail")
self.present(secondVC, animated: true, completion: nil)
}
return cell
}
向您的单元格添加委托并将其分配给演示 VC。见下文。
使用委派
创建一个继承自 UITableViewCell 的 CustomCell。
class CustomCell: UITableViewCell {
var cellDelegate : CustomCellDelegate = nil
@IBAction func elementTapped() {
cellDelegate?.launchVC()
}
}
自定义单元格代表
protocol CustomCellDelegate {
func launchVC()
}
主要ViewController
class ViewController: UIViewController, UITableViewControllerDataSource, UITableViewDelegate {
IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
tableView.dataSource = self
tableView.delegate = self
}
func numberOfSections(in: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? CustomCell {
// important
cell.delegate = self
return cell
}
}
}
扩展ViewController以实施协议
extension ViewContrller: CustomCellDelegate {
func launchVC() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
self.present(vc, animated: true, completion: nil)
}
}
我想在用户单击 tableviewcell 中的元素时显示一个新的 ViewController。但是,用于启动 VC 的标准代码在 tableview 单元格中甚至在助手 class 中都不起作用,因为 TVC 和助手 class 都无法呈现一个视图控制器。
这是帮助程序中的代码 class。无论是放置在 helperclass 还是 tableview 单元格中,它都没有启动 VC 的 present 方法。
class launchVC {
func launchVCNamed(identifier: String) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyBoard.instantiateViewController(withIdentifier: "contactDetail")
//FOLLOWING LINE HAS ERROR NO SUCH MEMBER (present)
self.present(secondVC, animated: true, completion: nil)
}
}
如何修改它以启动 VC?
通常您应该使用委托模式或闭包将块从单元格传回视图控制器。我更喜欢使用闭包而不是委托,所以我会举这样的例子:
class SomeCell: UITableViewCell {
var actionBlock = { }
func someActionOccured() { // some action like button tap in cell occured
actionBlock()
}
}
并且在 cellForRow
中,您需要在视图控制器中分配闭包
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SomeCell // replace cell identifier with whatever your identifier is
cell.actionBlock = { [unowned self] in
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyBoard.instantiateViewController(withIdentifier: "contactDetail")
self.present(secondVC, animated: true, completion: nil)
}
return cell
}
向您的单元格添加委托并将其分配给演示 VC。见下文。
使用委派
创建一个继承自 UITableViewCell 的 CustomCell。
class CustomCell: UITableViewCell {
var cellDelegate : CustomCellDelegate = nil
@IBAction func elementTapped() {
cellDelegate?.launchVC()
}
}
自定义单元格代表
protocol CustomCellDelegate {
func launchVC()
}
主要ViewController
class ViewController: UIViewController, UITableViewControllerDataSource, UITableViewDelegate {
IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
tableView.dataSource = self
tableView.delegate = self
}
func numberOfSections(in: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? CustomCell {
// important
cell.delegate = self
return cell
}
}
}
扩展ViewController以实施协议
extension ViewContrller: CustomCellDelegate {
func launchVC() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
self.present(vc, animated: true, completion: nil)
}
}