iOS开发storyboard拖拽tableView: Static cells的使用

来源:互联网 发布:2016年粮食进出口数据 编辑:程序博客网 时间:2024/05/09 21:22
从 object library 中,拖拽一个 UITableView 到 main.storyboard的 UIViewController 中; 
 设置  table view 的类型为:  Static Cells。  设置的方法: 选中 tableview, 在 attributes inspector 中设置。
  这时会报错。 原因是:  static tableView 只能潜在 UITableViewController中。 
  只好 创建一个 UITableViewController , 并将tableView设为 static cell。
注意了, 这时候,在屏幕的上方,会出现一个 错误提示:  illegal configuration : static table views are only valid when embedded in UITableViewController instances.

1.  拖拽一个 UITableViewController 到 storyboard中;
2.  创建一个 基于 UITableViewController 的 类文件;
2.  可以设置多个section, 也可以对每个section设置多行, 超出一屏,可上下滑动。

3.   在 document outline 中的 table view cell 中,出现 content view, 你所看到的所有cell 中的内容,就是在这个 content view 中 填充的。  content view, 顾名思义, 就是添加 view 的容器。

接下来,你就可以随心所欲地 在 cell 中 添加你的内容了。 
比如: 添加 Label; 添加 ImageView。 

通过这种简单的拖拽操作,你可以快速实现UI。 

友情提示:
在tableview 上,通过鼠标来操作,有时,你会感到力不从心。 这是因为, tableview 上的元素太复杂,包括: tableview section,  tableview cell, content view,  label, imageview 等。 怎么办呢?  通过左侧的 document outline 来选择, 这样就容易多了。 


iOS:  tableview static cell 的使用 - 悠悠 - 悠悠电台iOS客户端:几千个国内外电台
 
在 tableview cell 有多个属性可设置,比如:cell 选中后的状态, 右侧的指示标志。Accessory 的设置等。 

小结:
与 static cell 对应的是 dynamic cell, 二者的差异是什么? 使用场景又是怎样的呢 ?  这就是 静态与动态之分。 
二者的共同点: 支持多个section, 支持的cell 可以多于一屏,多于一屏时,可上下滑动。

不同点:static tableView 用不着 datasource  和 delegate。  动态tableView 是一定要重用cell的, 对于静态tableView, 似乎用不着 cell 的重用。 

这就是说,静态cell的功能 完全可以通过 动态cell 来实现。  当cell 数量超过一屏幕时, 建议还是使用 dynamic tableView 为好。 

小结: 
 很难说 静态tableView 与 动态TableView 有什么优劣之分, 这得看你用在哪种场景下。 不管是哪种技术,我个人坚信一点,那就是: “在一个给定的场景下, 最优方案只有一个” 。
这里,援引 苹果公司的官方文档,对static tableView 和 Dynamic TableView 做一个总结:

When you configure the attributes of a table view in the storyboard editor, you choose between two types of cell content: static cells or dynamic prototypes.

  • Static cells. Use static cells to design a table with a fixed number of rows, each with its own layout. Use static cells when you know what the table looks like at design time, regardless of the specific information it displays.

  • Dynamic prototypes. Use dynamic prototypes to design one cell and then use it as the template for other cells in the table. Use a dynamic prototype when multiple cells in a table should use the same layout to display information. Dynamic prototype content is managed by the data source at runtime, with an arbitrary number of cells.

阅读全文
0 0
原创粉丝点击