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,寫一些簡單代碼就能達到同樣效果這裡隻為記錄下一個解決辦法