Corona中文站

强大、易学的跨平台(iOS/Android)开发框架。QQ群1:74390406(满) 群2:221929599

导航

使用xib文件来快速创建自定义风格的table view[独家图文 ]
1。创建view-based工程图


2.用ib打开...controller.xib文件,拖入一个table view。



3。在classes目录下创建UITableView文件,取名为BagItemCell。


.h文件,.m文件不用改

#import

@interface BagItemCell : UITableViewCell {


IBOutlet
UILabel *lblName;

}

@property (nonatomic,retain) UILabel *lblName;

@end


4。在Resourses中创建xib文件,取名为BagItemCell。




设计table view cell界面,如下图


设置BagItemCell.xib文件与BagItemCell.h的关联,这个很重要,不能少


5。在tableviewcontroller.m文件中添加代码
.h

#import

@interface TableViewTestViewController : UIViewController {


NSArray *dataItems;

}

@end


.m

#import "TableViewTestViewController.h"

#import "BagItemCell.h"

@implementation TableViewTestViewController

- (void)viewDidLoad {


dataItems= [[NSArray
alloc] initWithObjects:@"大瓶生命药水",@"新手药水",@"赠送大礼包(15级可用)",@"粗布衣",@"帅气长裤",@"回复头巾",nil];

[super
viewDidLoad];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [dataItems count];

}

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {


return
@"我的背包";

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


static NSString *CellIdentifier = @"BagItemCell";

BagItemCell *cell = (BagItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {


NSArray *array = [[NSBundle
mainBundle] loadNibNamed:@"BagItemCell"
owner:self
options:nil];


cell = [array objectAtIndex:0];


[cell setSelectionStyle:UITableViewCellSelectionStyleGray];

}


NSUInteger row = [indexPath row];


[cell.lblName
setText:[dataItems
objectAtIndex:row]];

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


return
80;

}

- (void)didReceiveMemoryWarning {

[super
didReceiveMemoryWarning];

}

- (void)viewDidUnload {


// Release any retained subviews of the main view.


// e.g. self.myOutlet = nil;

}

- (void)dealloc {

[super dealloc];

}

@end
<< iPhone 异步加载图片[附代码] NSMutableURLRequest实现向web服务器POST JSON数据 >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Copyright 2011-2015 BuildApp.Net. All Rights Reserved.