ios 7 customizing UITableViewCell's content view

落花浮王杯 提交于 2019-12-12 07:40:07

问题


Using storyboard in ios7 the content view is explicitly viewed under the Table View Cell in the story board editor (opposed to previous versions where it was hidden from the interface).

The problem is that I cannot connect the cell's custom elements to my custom cell's ib outlets anymore (subclass of UITableCellView) and can only connect them to the table's content view (UIView) which CANNOT be customized (i.e. its custom class attribute is grayed)

This leaves me with the option to use tags which I find inconvenient and less elegant.

I could also create a custom view in a xib, do all the connections and then manually add this custom view as the content view of the table but I am looking for a way doing it via the story board editor.

Is there a way to connect UI elements to a custom cell's content view in the story board editor in ios7 ?


回答1:


I was able to do this by doing the following:

  1. Select the cell in the document outline
  2. Change its custom class in the Identity Inspector
  3. Place whatever elements you want into the content view
  4. Connect the IBOutlets to the elements inside the content view using the Connections Inspector



回答2:


Ok I found an Xcode bug.

If you complete the following this will replicate the issue:
- Create new UIViewController in storyboard
- Drag a UITableView to the VC
- Update the UITableview to have 1(as many) dynamic prototype cells
ISSUE: The cells are added but without a contentView.

RESOLUTION: Rather than updating the amount of cells in the storyboard. Drag a custom cell from the objects part of Xcode, the Cell will be added with a contentView.




回答3:


I don't really know the answer, but I can suggest work around this issue:

1) Copy existing cell from other tableview to the one you're working on.

2) You will have contentView under your cell now. Design this cell by adding your views.

3) Create a class for your cell, e.g NewCell, then manually create IBOutlet in this class:

@property (nonatomic, strong) IBOutlet UILabel* mainLabel;

4) Assign the cell class in storyboard to the class you just created. After this step, you can drag the outlet from storyboard to class.

I'm not sure if this is a bug for XCode 5 or it is intended, and I'm looking for better solutions as well.




回答4:


Create new uiviewcontroller with xib interface and add the required sub-views .Also,establish the iboutlet connections.

Now ,rename the viewcontroller : UIViewcontroller to viewcontroller : UITableviewcell in .h file of your newly created view controller.

Your tableviewcell was created and ready to use with any UITableview of any class.

Hope it helps.




回答5:


I might be duplicating someone else's answer but I found this workaround which helped bypass this silly XCode bug. Short solution: When you build your custom cell in Storyboard, do NOT drag your UI elements inside the cell or content view.

Instead, drag them OUTSIDE so that they're child elements of the parent table view!

It's easiest to do this drag and drop if you use the little hierarchy menu on the left to make sure your elements wind up in the right spot.

Once the elements are dragged in, just control-click and drag from the custom cell to the UI elements to make your connections. Woo hoo!

Once your connections are set, then (finally) drag the UI elements back INTO the custom cell's content view and lay them out as you normally would.

UPDATE: While my method works, look for a comment by thomasdao in an answer below where he just drags the connections directly into the .h file (the little circles in the left margin where you declare your IBOutlets) -- this is actually the easiest solution but unfortunately it's stuck as a comment.



来源:https://stackoverflow.com/questions/19196868/ios-7-customizing-uitableviewcells-content-view

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