以编程方式生成不同的静态 UITableviews
programmatically generate different static UITableviews
我刚刚加入 Whosebug,我正在努力满足我在 swift 中开发的 iPhone 应用程序的编程要求。我有一个 table 查看不同计算器的列表,我想在单击某个项目时转到另一个 UITableView
,然后对该工具进行详细计算。
我可能在第一个 table (>20) 中有很多工具,所以我不想使用故事板来绘制每个新的 UITableView
静态 table 每个都有不同的 segue。
我想知道是否有人可以就如何在单击某个项目时以编程方式编写带有静态单元格的新 UITableViewController
演示文稿的一些建议。我不想使用故事板,所以我需要使用代码来管理演示文稿以及使用静态单元格生成下一个 UITableViewController
。
我已经能够以编程方式使用自定义 class 编写静态 table 视图并链接到故事板 UITableViewController
项目,但我想以编程方式完成所有这些操作并删除故事板全部放在一起。
如有任何建议,我们将不胜感激。
谢谢。
一个UITableViewController
抽象了一些东西。看起来您可能想要做的是将事物分开并进行更精细的控制。
您可以很容易地做到这一点。您需要 3 件事才能实现这一目标:
UITableView
UITableViewDataSource
UITableViewDelegate
A UITableViewController
为您整理了这些。我们必须自己创建它们。
为此,我们创建一个视图控制器,并继承 UITableViewDataSource
和 UITableViewDelegate
class Example: UIViewController {
}
// MARK - UITableViewDataSource
extension Example: UITableViewDataSource {
// We need to implement some methods here
}
// MARK - UITableViewDelegate
extension Example: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Handle the user clicking an item here
}
}
还有三件事要做:
- 创建并显示 table
- 获取数据显示在table
- 实现委托方法
创建 table
您应该决定是要完全以编程方式创建 UITableView,还是让 Interface Builder 在 .xib 中为您布置一个,然后您只需 link 通过 IBOutlet 将其创建。
假设你想做前者,你可以这样做:
var table: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
table = UITableView(frame: view.bounds)
view.addSubview(table!)
table?.delegate = self
table?.dataSource = self
}
获取数据
当您从以前的视图控制器推送此视图控制器时,请务必使用您的数据在此视图控制器上设置一个变量。假设你有一个数组,它就像这样简单:
exampleViewController.myData = someArray;
navigationController?.pushViewController(exampleViewController, animated: true)
(一定要在你的视图控制器中创建 myData 变量来获取它)
实现委托方法
现在我们可以实现委托方法来显示数据。你可能已经很熟悉了,但为了完整起见:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// You should really use dequeueReusableCellWithIdentifier here.
var cell = UITableViewCell()
cell.textLabel!.text = myData[indexPath.row]
return cell
}
我刚刚加入 Whosebug,我正在努力满足我在 swift 中开发的 iPhone 应用程序的编程要求。我有一个 table 查看不同计算器的列表,我想在单击某个项目时转到另一个 UITableView
,然后对该工具进行详细计算。
我可能在第一个 table (>20) 中有很多工具,所以我不想使用故事板来绘制每个新的 UITableView
静态 table 每个都有不同的 segue。
我想知道是否有人可以就如何在单击某个项目时以编程方式编写带有静态单元格的新 UITableViewController
演示文稿的一些建议。我不想使用故事板,所以我需要使用代码来管理演示文稿以及使用静态单元格生成下一个 UITableViewController
。
我已经能够以编程方式使用自定义 class 编写静态 table 视图并链接到故事板 UITableViewController
项目,但我想以编程方式完成所有这些操作并删除故事板全部放在一起。
如有任何建议,我们将不胜感激。
谢谢。
一个UITableViewController
抽象了一些东西。看起来您可能想要做的是将事物分开并进行更精细的控制。
您可以很容易地做到这一点。您需要 3 件事才能实现这一目标:
UITableView
UITableViewDataSource
UITableViewDelegate
A UITableViewController
为您整理了这些。我们必须自己创建它们。
为此,我们创建一个视图控制器,并继承 UITableViewDataSource
和 UITableViewDelegate
class Example: UIViewController {
}
// MARK - UITableViewDataSource
extension Example: UITableViewDataSource {
// We need to implement some methods here
}
// MARK - UITableViewDelegate
extension Example: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Handle the user clicking an item here
}
}
还有三件事要做:
- 创建并显示 table
- 获取数据显示在table
- 实现委托方法
创建 table
您应该决定是要完全以编程方式创建 UITableView,还是让 Interface Builder 在 .xib 中为您布置一个,然后您只需 link 通过 IBOutlet 将其创建。
假设你想做前者,你可以这样做:
var table: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
table = UITableView(frame: view.bounds)
view.addSubview(table!)
table?.delegate = self
table?.dataSource = self
}
获取数据
当您从以前的视图控制器推送此视图控制器时,请务必使用您的数据在此视图控制器上设置一个变量。假设你有一个数组,它就像这样简单:
exampleViewController.myData = someArray;
navigationController?.pushViewController(exampleViewController, animated: true)
(一定要在你的视图控制器中创建 myData 变量来获取它)
实现委托方法
现在我们可以实现委托方法来显示数据。你可能已经很熟悉了,但为了完整起见:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// You should really use dequeueReusableCellWithIdentifier here.
var cell = UITableViewCell()
cell.textLabel!.text = myData[indexPath.row]
return cell
}