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