您的位置:首页 > 产品设计 > UI/UE

Xib文件创建UITableViewCell

2014-08-22 11:39 204 查看

iPhone
开发中关于Xib文件创建UITableViewCell是本文要介绍的内容,主要是来学习如何使用XIB文件创建UITableViewCell的几种方法,来看本文详细内容。1、cell不做为controller的插口变量首先创建一个空的xib文件,然后拖拽一个cell放在其上面,记得设置其属性Identifier,假设设置为“mycell”,则我们在代码中请求cell的时候就应该如下写:
NSString *identifier = @"mycell";UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier: identifier];if (!cell) {cell = [[[NSBundle mainBundle] loadNibNamed:@"TVCell"
owner:self options:nil] lastObject];}return cell;2、cell做为controller的插口变量声明的时候应该写
@property (nonnonatomic, assign) IBOutlet UITableViewCell *tvCell;@synthesize tvCell创建nib文件的时候要把file owner选择成当前的controller,然后把IBOut连接起来。cellForRowAtIndexPath函数实现为:
static NSString *MyIdentifier = @"MyIdentifier";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];if (cell == nil) {[[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];cell = tvCell;self.tvCell = nil;}我们可以通过UINib来提高性能使用UINib类可以大大的提高性能,正常的nib 加载过程包括从硬盘读取nib
file,并且实例化对象。但是当我们使用UINib类时,nib
文件只用从硬盘读取一次,读取之后,内容存在内存中。因为其在内存中,创建一序列的对象会花费很少的时间,因为其不再需要访问硬盘。头文件中:
ApplicationCell *tmpCell;// referring to our xib-based UITableViewCell ('IndividualSubviewsBasedApplicationCell')UINib *cellNib;@property (nonnonatomic, retain) IBOutlet ApplicationCell *tmpCell;@property (nonnonatomic, retain) UINib *cellNib;viewDidLoad中:
self.cellNib = [UINib nibWithNibName:@"IndividualSubviewsBasedApplicationCell" bundle:nil];cellForRowAtIndexPath实现:
static NSString *CellIdentifier = @"ApplicationCell";ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil){[self.cellNib instantiateWithOwner:self options:nil];cell = tmpCell;self.tmpCell = nil;}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.view plain- (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.view plain- (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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: