任务1.负载的UIViewController形式XIB
我们可以加载一个UIViewController形式XIB代替故事板。我们可以使用以下程序:
1.创建一个UIViewcontroller。
XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your class name , subclass of with UIViewController , check Also create Xib file, language Swift -> Next - Create.
Example: ViewControllerFromXib
2.覆盖的init()。
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
{
super.init(nibName: "ViewControllerFromXib", bundle: Bundle.main)
}
required init?(coder aDecoder: NSCoder)
{
super.init(coder:aDecoder)
}
3.打开新创建的控制器
let controller = ViewControllerFromXib.init()
self.present(controller, animated: true, completion: nil)
在这上面的方法,我们可以从厦门国际银行加载一个UIViewController。
任务2.创建一个实现代码如下&它使用电池定制厦门国际银行
1.创建一个自定义的UITableViewCell
的XCode文件填充 - >新建 - >文件 - >可可触摸类 - >用你的单元格名称填充类,使用TableViewCell的子类,检查另外创建Xib文件,Swift语言 - >下一步 - 创建。
例子:CustomTableViewCell
1.注册的UITableViewCell为您的TableView。
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Item"
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle:Bundle.main), forCellReuseIdentifier: "CustomTableViewCell");
}
2.实施UITableViewDataSource到你的ViewController
extension ViewControllerFromXib:UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
return cell
}
}
2.实施的UITableViewDelegate到你的ViewController。
extension ViewControllerFromXib:UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension;
}
}
你创建的东西看起来像一个表视图而不是一个表视图控制器。这就是他们与众不同的原因。另外,没有什么能够阻止你创建另一个故事板。随着项目的发展,这也非常普遍。 –
嗨雨辰,这是为了表格视图而不是表格视图控制器。为什么人们在他们的项目中有更多的故事板,为什么它是有用的和常见的? –
故事板是基于xml的,当有多个人在同一时间工作时,它可以很容易地导致冲突。冲突很难解决。另外,故事板渲染速度很慢,难以处理,因为它变得更复杂。所以,让它们变小会更容易。 –