您的位置:首页 > 其它

tableView头部放scrollView滚动视图

2015-10-18 16:32 435 查看
开发思想 自定义UIView ,在UIView中添加scrollView,

使用场景 新闻首页滚动效果 so on

//

// BookViewController.m

// UITableViewCell定制

#import "BookViewController.h"

#import "BookModel.h"

#import "BookCell.h"

#import "ADView.h"

@interface BookViewController ()<UITableViewDataSource,UITableViewDelegate>

{

UITableView *_bookTableView;

//数据源数组

NSMutableArray *_dataArray;

}

@end

@implementation BookViewController

- (void)dealloc{

[_dataArray release];

[_bookTableView release];

[super dealloc];

}

- (void)viewDidLoad

{

[super viewDidLoad];

self.automaticallyAdjustsScrollViewInsets = NO;

[self loadData];

[self showUI];

}

//初始化数据

- (void)loadData{

_dataArray = [[NSMutableArray alloc] init];

//解析plist 文件

NSString *path = [[NSBundle mainBundle] pathForResource:@"bookData" ofType:@"plist"];

NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

//遍历数组 把数组中字典的内容 存放到数据模型对象中 然后把数据模型对象地址放入数据源数组

for (NSDictionary *dict in array) {

BookModel *bookModel = [[BookModel alloc] init];

bookModel.bookName = [dict objectForKey:@"title"];

bookModel.bookDescription = dict[@"detail"];

bookModel.price = dict[@"price"];

bookModel.imageName = dict[@"icon"];

[_dataArray addObject:bookModel];

[bookModel release];

}

}

//创建表格视图

#pragma mark - 创建表格视图

- (void)showUI{

_bookTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 416) style:UITableViewStylePlain];

//设置代理

_bookTableView.delegate = self;

_bookTableView.dataSource = self;

//_bookTableView.transform = CGAffineTransformMakeRotation(-M_PI_2);

//设置表格的头视图

_bookTableView.tableHeaderView = [[[ADView alloc] initWithFrame:CGRectMake(0, 0,320,160)] autorelease];

_bookTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

[self.view addSubview:_bookTableView];

}

#pragma mark == delegate协议==

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

return 100;

}

#pragma mark - 数据源协议的方法

//多少行

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

return _dataArray.count;

}

//创建cell

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

static NSString * cellName = @"bookCell";

//从复用队列中获取自定义的cell

BookCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];

if (cell == nil) {

//创建自定义的cell

cell = [[BookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];

//cell.transform = CGAffineTransformMakeRotation(M_PI_2);

}

//对cell 进行内容的修改 在{}进行

//从数据源获取数据模型

BookModel *model = _dataArray[indexPath.row];

//先设置数据模型

cell.bookModel = model;

//显示数据

[cell showData];

cell.backgroundColor = [UIColor colorWithRed:154/255.0f green:154/255.0f blue:154/255.0f alpha:1];

return cell;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

NSLog(@"tableView gun ");

}

@end

//
//  ADView.m

#import "ADView.h"
#import "MyControl.h"

@interface ADView()  <UIScrollViewDelegate>

@end

@implementation ADView
{
UIScrollView *_scrollView;
UIPageControl *_pageControl;
UILabel *_label;
}
- (void)dealloc{
[_scrollView release];
[_pageControl release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self showUI];
}
return self;
}
- (void)showUI{

_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
for (int i = 0; i < 4; i++) {
UIImageView *image = [MyControl creatImageViewWithFrame:CGRectMake(320*i, 0, 320, 160) image:[NSString stringWithFormat:@"image%d",i]];
[_scrollView addSubview:image];
}
_scrollView.contentSize = CGSizeMake(320*4, 160);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
[self addSubview:_scrollView];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 130, 320, 30)];
view.backgroundColor = [UIColor blackColor];
view.alpha = 0.5;

_label = [MyControl creatLabelWithFrame:CGRectMake(10, 0, 200, 30) text:@"图片0的标题"];
_label.textColor = [UIColor whiteColor];
[view addSubview:_label];

_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(200, 0, 120, 30)];
_pageControl.numberOfPages = 4;
//添加翻页触发事件
[_pageControl addTarget:self action:@selector(pageControl:) forControlEvents:UIControlEventValueChanged];
[view addSubview:_pageControl];
[self addSubview:view];
[view release];

}
//控制滚动
- (void)pageControl:(UIPageControl *)page{
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES];
_label.text = [NSString stringWithFormat:@"图片%ld的标题",page.currentPage];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGPoint point = scrollView.contentOffset;
_pageControl.currentPage = point.x/320;
_label.text = [NSString stringWithFormat:@"图片%ld的标题",_pageControl.currentPage];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"ScrollView gun ");
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

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