Using Interface Builder for UITableViews

落花浮王杯 提交于 2019-12-30 00:44:08

问题


I'm very early in the iPhone development learning process. I'm trying to get my head around various pieces. Right now I've just taken the basic NavigationController template and I'm trying to create a simple grouped table view with a couple of text fields. What I can't seem to do is to get Interface Builder to allow me to drop a UITableViewCell into a UITableView so that I can then add a text field to the Cell. Is this even possible (it would seem that its supposed to be given the fact that UITableViewCell is a draggable control)?

If not, does that mean all of that is code I will need to just write myself?


回答1:


You can create the cell with Interface Builder, but you have to make it a top-level object, rather than a child of the table view. Then you can return this cell in your view controller's tableView:cellForRowAtIndexPath: function.

Make sure to give the cell an identifier in Interface Builder and then use the same identifier with dequeueReusableCellWithIdentifier: (see the sample code for how this works -- the idea is that cells get re-used - the OS will only allocate as many cells as fit on the screen at once. Clever way to save memory.)




回答2:


Be careful with Boot To The Head's method. You will leak if you don't properly deal with your IBOutlets. I will try to explain this to the best of my ability without posting code (NDA). If you plan on using IB to create your cell, make the UITableViewCell it's own Xib file. Set the File's Owner as your UIViewController subclass (or UITableController). Call the IBOutlet something like UITableViewCell *cellFactory. In the UITableViewDataSource method tableView:cellForRowAtIndexPath: do the following pseudo-code;

  1. Try to dequeue a cell using the identifier you setup in IB
  2. If successful, your done. Just use the cell
  3. Else you need to create a new cell. Use the [NSBundle mainBundle] loadNibNamed:owner:options: method with your proper xib file in there. This will fill the cellFactory ivar with a fresh cell. Here comes the tricky part.
  4. set cell = cellFactory then release cellFactory and set it to nil to be sure you don't accidentally use it again. You are now safe to use your cell as normal



回答3:


Unfortunately, it doesn't really work that way - the cells in the tableview are generated by the delegate at run-time. It turns out to be very straightforward code, though. Check out the tableview example code, it's pretty easy to follow.




回答4:


This is a good tutorial on using the UI builder for UITableView's



来源:https://stackoverflow.com/questions/134575/using-interface-builder-for-uitableviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!