切换到 UITableViewController 时的调用方法
Calling method when switching to UITableViewController
我设法以编程方式切换到 UITableViewController。但是,是否有初始方法或类似性质的东西,我可以在切换后立即 运行 ?无论 table 中是否有数据,我都希望代码为 运行。
Swift 的新手,感谢任何帮助。
编辑:
更具体地说,在我的主要 ViewController 中,我正在这样做:
var next = self.storyboard?.instantiateViewControllerWithIdentifier("MyEvents") as! MyEvents
self.presentViewController(next, animated: true, completion: nil)
据我了解这是推表ViewController
然后class表专用ViewController:
import UIKit
class MyEvents: UITableViewController, SideBarDelegate {
var eventList = [];
var sideBar:SideBar = SideBar()
required init!(coder aDecoder: NSCoder!) {
self.init()
sideBar = SideBar(sourceView: self.view, menuItems: ["My Events","Support","Settings"])
sideBar.delegate = self }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return eventList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return basicCellAtIndexPath(indexPath)
}
func basicCellAtIndexPath(indexPath:NSIndexPath) -> BasicCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! BasicCell
setCellValues(cell, indexPath: indexPath)
return cell
}
func setCellValues(cell:BasicCell, indexPath:NSIndexPath)
{
cell.eventLabel.text = "Test Event"
cell.acceptLabel.text = "100"
cell.declineLabel.text = "90"
cell.acceptSubLabel.text = "Accepts"
cell.declineLabel.text = "Decline"
}
func sideBarDidSelectButtonAtIndex(index: Int)
{
if index == 0
{
println("Here!")
}
}
}
我认为您应该在问题中提供更多细节。当你对 UITableViewController 说 'switch' 时,你是说你正在从另一个 UITableViewController 推送这个 UITableViewController 并且它现在是你屏幕上的主要 VC 吗?
您需要在 UITableViewController 代码中实现一些方法; numberOfSections、numberOfRowsInSection、cellForRowAtIndexPath 等。基本上,您需要遵守 UITableViewDataSource 协议(您可以在 Apple 的文档中查找)。
再详细一点,我们就能为您提供更多帮助!
如果您创建了 UITableViewController 的子类,则可以将任何初始化代码放入(覆盖)方法:viewDidLoad。请参阅下面的示例,了解您需要的其他强制方法:
import UIKit
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Test"
self.navigationItem.rightBarButtonItem = self.editButtonItem()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cell")
let xrow = indexPath.row
let xsection = indexPath.section
println("row = \(xrow), section = \(xsection)")
println("0: cell.textLabel?.text = \(cell!.textLabel?.text)")
cell!.textLabel?.text = "Section = \(xsection)"
println("1: cell.textLabel?.text = \(cell!.textLabel?.text)")
println("0: cell.detailTextLabel?.text = \(cell!.detailTextLabel?.text)")
cell!.detailTextLabel?.text = ("section = \(xsection), row = \(xrow)") as String!
println("1: cell.detailTextLabel?.text = \(cell!.detailTextLabel?.text)")
return cell!
}
}
您可以使用 viewDidAppear
或 viewWillAppear
。使用语法
override func viewWillAppear() {
super.viewWillAppear()
//Code
}
希望对您有所帮助!
我设法以编程方式切换到 UITableViewController。但是,是否有初始方法或类似性质的东西,我可以在切换后立即 运行 ?无论 table 中是否有数据,我都希望代码为 运行。
Swift 的新手,感谢任何帮助。
编辑:
更具体地说,在我的主要 ViewController 中,我正在这样做:
var next = self.storyboard?.instantiateViewControllerWithIdentifier("MyEvents") as! MyEvents
self.presentViewController(next, animated: true, completion: nil)
据我了解这是推表ViewController
然后class表专用ViewController:
import UIKit
class MyEvents: UITableViewController, SideBarDelegate {
var eventList = [];
var sideBar:SideBar = SideBar()
required init!(coder aDecoder: NSCoder!) {
self.init()
sideBar = SideBar(sourceView: self.view, menuItems: ["My Events","Support","Settings"])
sideBar.delegate = self }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return eventList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return basicCellAtIndexPath(indexPath)
}
func basicCellAtIndexPath(indexPath:NSIndexPath) -> BasicCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! BasicCell
setCellValues(cell, indexPath: indexPath)
return cell
}
func setCellValues(cell:BasicCell, indexPath:NSIndexPath)
{
cell.eventLabel.text = "Test Event"
cell.acceptLabel.text = "100"
cell.declineLabel.text = "90"
cell.acceptSubLabel.text = "Accepts"
cell.declineLabel.text = "Decline"
}
func sideBarDidSelectButtonAtIndex(index: Int)
{
if index == 0
{
println("Here!")
}
}
}
我认为您应该在问题中提供更多细节。当你对 UITableViewController 说 'switch' 时,你是说你正在从另一个 UITableViewController 推送这个 UITableViewController 并且它现在是你屏幕上的主要 VC 吗?
您需要在 UITableViewController 代码中实现一些方法; numberOfSections、numberOfRowsInSection、cellForRowAtIndexPath 等。基本上,您需要遵守 UITableViewDataSource 协议(您可以在 Apple 的文档中查找)。
再详细一点,我们就能为您提供更多帮助!
如果您创建了 UITableViewController 的子类,则可以将任何初始化代码放入(覆盖)方法:viewDidLoad。请参阅下面的示例,了解您需要的其他强制方法:
import UIKit
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Test"
self.navigationItem.rightBarButtonItem = self.editButtonItem()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cell")
let xrow = indexPath.row
let xsection = indexPath.section
println("row = \(xrow), section = \(xsection)")
println("0: cell.textLabel?.text = \(cell!.textLabel?.text)")
cell!.textLabel?.text = "Section = \(xsection)"
println("1: cell.textLabel?.text = \(cell!.textLabel?.text)")
println("0: cell.detailTextLabel?.text = \(cell!.detailTextLabel?.text)")
cell!.detailTextLabel?.text = ("section = \(xsection), row = \(xrow)") as String!
println("1: cell.detailTextLabel?.text = \(cell!.detailTextLabel?.text)")
return cell!
}
}
您可以使用 viewDidAppear
或 viewWillAppear
。使用语法
override func viewWillAppear() {
super.viewWillAppear()
//Code
}
希望对您有所帮助!