iOS storyboard 任意使用static tableView
iOS的布局中,可以使用 static tableView cell即无需编程,直在在sb中一个一个画出cell的布局来,这是真正的所见即所得。
这种方法在UITableViewController 对应的窗口很好用,但是如果你想美化一下界面,比如想把TableView 加入到另一个View中,上下加点标题之类美化。 此时也是能设计出效果,但是执行时会提示编译错误:
error: Illegal Configuration: Static table views are only valid when embedded in UITableViewController instances
怎么办?
解决办法
有人提到的解决办法是曲线设计,即先做一个tableViewController然后再把这个view 嵌入到你真正需要显示的view当中:
http://stackoverflow.com/questions/22364230/static-table-view-outside-uitableviewcontroller
原文内容如下:
- Drag a ViewController onto your storyboard.
- Drag a TableViewController onto your storyboard.
- Next Drag a Container view to your ViewController and size it about the size you want (smaller than the view) -> when you drag the container view it will create a segue and another view. Remove that segue and view.
- Then finally ctrl click in your container and drag to your new TableViewContoller. Select Embed.
- Style your TableView the way you want -> including static cells.
翻译意思如下
你真正显示是storyboard中一个viewController,假设为viewA
为了使用静态cell,你还得多拖出来一个UITableViewController 到storyboard上,假设为viewB
在viewA拖一个container view控件,按ctrl拖向viewB,此时会弹出一个菜单,选择embed ,即表示在viewA
中嵌入显示viewB
题外话
做完后发现,仅仅为一个静态cell就费这么多功能并不值得,
因为你完全可以做同等数量的cell原型,在创建cell,写一些简单代码就能达到同样效果这里只为记录下一个解决办法