使用XIB文件来定制你的UITableViewCell
2011-05-31 09:17
253 查看
Looking around the App Store, I see most apps customize theirUITableViews in a unique way.
Flixster embeds movie posters and ratings, in addition to their titles.
Tweetie integrates tweets, icons, usernames, and the date.
GasBuddy lists service type, amount spent, gallons, and dollars per gallon in each row.
Constructing these customizedUITableViewCells is possible in code, but leveraging Interface Builder’s drag-and-drop interface is far more fun.
Thanks to Bill Dudney for talking about one approach to this on his blog and to StackOverflow for covering this topic.
Creating a custom UITableViewCell using Interface Builder is straight-forward.
In Xcode, create a new UITableViewCell subclass and add the desired IBOutlets to the header file.
In Interface Builder, create an “Empty” XIB from the Cocoa Touch palette.
Drag a UITableViewCell from the Library into it, configure the class to be your new custom UITableViewCell subclass, and give it the appropriate identifier.
Add the desired elements to the UITableViewCell and connect the subclass’s outlets to them.
To instantiate the cells using the UIViewController approach, set class of the new XIB’s “File’s Owner” to be “UIViewController”, and connect its view outlet to the customized UITableViewCell.
The XIB is set up, but there are two approaches to instantiating the new customized cell from that XIB. When I was at WWDC a couple weeks ago, I confirmed with one of the Interface Builder engineers at the IB Lab that both work just fine. (He did repeatedly ask if I was usingUITableView:dequeueReusableCellWithIdentifier:, just to make sure.)
UIViewController
One approach is to create a temporary UIViewController each time you need a new cell. By setting up the XIB the way we did, the temporaryUIViewController has the cell as its view attribute. After we grab a pointer to that, we can release the view controller.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"]; if (cell == nil) { // Create a temporary UIViewController to instantiate the custom cell. UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"BDCustomCell" bundle:nil]; // Grab a pointer to the custom cell. cell = (BDCustomCell *)temporaryController.view; // Release the temporary UIViewController. [temporaryController release]; } return cell; }
NSBundle:loadNibNamed:owner:options:
Another approach is to load the NIB file and grab the cell directly, as it’s the only top-level object in the NIB.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"]; if (cell == nil) { // Load the top-level objects from the custom cell XIB. NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil]; // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). cell = [topLevelObjects objectAtIndex:0]; } return cell; }
相关文章推荐
- storyboard中用xib文件创建定制的UITableViewCell对象 .
- ios – 使用UINib加载xib文件实现UITableViewCell
- 使用xib文件创建UITableViewCell
- 使用UINib加载xib文件实现UITableViewCell
- ios使用UINib加载xib文件实现UITableViewCell
- ios – 使用UINib加载xib文件实现UITableViewCell
- storyboard中用xib文件创建定制的UITableViewCell对象
- ios – 使用UINib加载xib文件实现UITableViewCell
- ios – 使用UINib加载xib文件实现UITableViewCell
- iOS 6 开发----应用集合视图(UICollectionView)-使用xib文件创建集合视图单元格
- 使用自定义的xib文件进行启动页面的加载
- RailsCasts中文版,#8 Layouts and content_for 使用content_for方法定制布局文件
- 进击的KFC:在同一个Xib内创建多个UITableViewCell来方便使用
- Xib文件使用(一)——xib文件解析
- iPhone开发中关于Xib文件创建UITableViewCell方法
- iOS UI基础-4.2应用程序管理 Xib文件使用
- IOS中使用.xib文件封装一个自定义View
- iOS 使用xib创建UITableViewCell的时候引用另外一个UITableViewCell时报错
- (私人简单记录)使用xib文件
- 利用 UML 概要文件 (UML Profiles) 定制领域建模,第 2 部分: 使用 Profile Tooling 向导