您的位置:首页 > 移动开发 > Objective-C

Objective---C 给tableView添加头视图轮播图

2015-12-22 23:45 302 查看
// 设置属性

@property(nonatomic,retain)UITableView *tableView;

@property(nonatomic,retain)UIImageView *imageView;

@property(nonatomic,retain)NSMutableArray *arr;

// 初始化数组

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if (self) {

self.arr = [NSMutableArray
arrayWithObjects:@"宋江1",
@"卢俊义2",
@"吴用3",
@"公孙胜4",
@"关胜5",
@"林冲6",
@"秦明7" ,@"呼延灼8" ,
@"花容9",@"柴进10",
@"李应11",
@"朱仝12",@"鲁智深13",@"武松14",@"张三",@"李四",@"王五",@"田七",@"小刘",@"老谭",nil];

}

return
self;

}

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.

self.tableView = [[UITableView
alloc] initWithFrame:self.view.frame
style:UITableViewStylePlain];

self.tableView.delegate =
self;

self.tableView.dataSource =
self;

[self.view
addSubview:_tableView];

[_tableView
release];

//
给tableView添加头视图轮播图

//
方式一

// 设置尺寸时,前三个数值没有影响

// self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];

// self.imageView.image = [UIImage imageNamed:@"type.png"];

// self.tableView.tableHeaderView = self.imageView;

// [_imageView release];

//
方式二

self.imageView = [[UIImageView
alloc] initWithFrame:CGRectMake(0, -200,
self.tableView.frame.size.width, 200)];

self.imageView.image = [UIImage
imageNamed:@"type.png"];

//
平移图片

self.tableView.contentInset =
UIEdgeInsetsMake(200, 0, 0, 0);

[self.tableView
addSubview:_imageView];

[_imageView
release];

}

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

return
self.arr.count;

}

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

static NSString *reuse =
@"reuse";

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:reuse];

if (!cell) {

cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:reuse] autorelease];

}

cell.textLabel.text =
self.arr[indexPath.row];

return cell;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: