首先看下效果图
最外层是一个uitableview,广告栏是一个uiscrollview,之前做过类似的例子,是在构造cell的方法
cellForRowAtIndexPath中判断行数再添加uiscrollview,但是觉得代码不够整洁,总之是怎么看怎么不爽,于是决定尝试用uitableview head来解决,既然要用到head,那肯定就是要自定义一个view了
以下是view的h文件
@interface AdvertHeaderView : UIView<UIScrollViewDelegate>{
int TimeNum;
BOOL Tend;
NSArray *imageArray;
}
@property (retain, nonatomic) UIScrollView *sv;
@property (retain, nonatomic) UIPageControl *PageControl;
- (void)configAdvert:(NSArray *) imgArray;
-(void)configUserInterface;
@end
以下是view的m文件
//
// AdvertHeaderView.m
// SUNCommonComponent
//
// Created by kobeli on 13-11-25.
// Copyright (c) 2013年 中山市新联医疗科技有限公司. All rights reserved.
//
#import "AdvertHeaderView.h"
@implementation AdvertHeaderView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
imageArray =自己定义一个装载图片地址的数组
[self configUserInterface];
[self configAdvert:imageArray];
}
return self;
}
-(void)configAdvert:(NSArray *)imgArray
{
[self AdImg:imgArray];
[self setCurrentPage:_PageControl.currentPage];
}
-(void)configUserInterface{
_sv=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 144)];
_sv.delegate=self;
_sv.showsHorizontalScrollIndicator=NO;
_sv.showsVerticalScrollIndicator=NO;
_sv.pagingEnabled=YES;
[self addSubview:_sv ];
_PageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(320/2-15, _sv.frame.size.height-23 , 30, 30)];
[self addSubview:_PageControl ];
}
-(void)AdImg:(NSArray*)arr{
[_sv setContentSize:CGSizeMake(320*[arr count], 144)];
_PageControl.numberOfPages=[arr count];
for ( int i=0; i<[arr count]; i++) {
NSString *url=[arr objectAtIndex:i];
UIButton *img=[[UIButton alloc]initWithFrame:CGRectMake(320*i, 0, 320, 144)];
// [img addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
[_sv addSubview:img];
//[img setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];
UIImageFromURL( [NSURL URLWithString:url], ^( UIImage * image )
{
[img setBackgroundImage:image forState:UIControlStateNormal];
}, ^(void){
});
}
}
void UIImageFromURL( NSURL * URL, void (^imageBlock)(UIImage * image), void (^errorBlock)(void) )
{
dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^(void)
{
NSData * data = [[NSData alloc] initWithContentsOfURL:URL] ;
UIImage * image = [[UIImage alloc] initWithData:data];
dispatch_async( dispatch_get_main_queue(), ^(void){
if( image != nil )
{
imageBlock( image );
} else {
errorBlock();
}
});
});
}
#pragma mark - 5秒换图片
- (void) handleTimer: (NSTimer *) timer
{
if (TimeNum % 5 == 0 ) {
if (!Tend) {
_PageControl.currentPage++;
if (_PageControl.currentPage==_PageControl.numberOfPages-1) {
Tend=YES;
}
}else{
_PageControl.currentPage--;
if (_PageControl.currentPage==0) {
Tend=NO;
}
}
[UIView animateWithDuration:0.8 //速度0.7秒
animations:^{//修改坐标
_sv.contentOffset = CGPointMake(_PageControl.currentPage*320,0);
}];
}
TimeNum ++;
}
#pragma mark - scrollView && page
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"我滑动了");
_PageControl.currentPage=scrollView.contentOffset.x/320;
[self setCurrentPage:_PageControl.currentPage];
}
- (void) setCurrentPage:(NSInteger)secondPage {
for (NSUInteger subviewIndex = 0; subviewIndex < [_PageControl.subviews count]; subviewIndex++) {
UIImageView* subview = [_PageControl.subviews objectAtIndex:subviewIndex];
//这里犯了一个小错误,如果不加类型判断的话会报错找不到setimage方法的错,所以一定要
判断是uiview还是uiimageview
if ([subview isKindOfClass:[UIImageView class]]) {
CGSize size;
size.height = 12/2;
size.width = 12/2;
[subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y,
size.width,size.height)];
if (subviewIndex == secondPage)
[subview setImage:[UIImage imageNamed:@"a.png"]];
else
[subview setImage:[UIImage imageNamed:@"d.png"]];
}
}
}
@end
uiview定义好了,就开始放进我们的uitableview里面去了,可以使用
//头标签高度设置
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat f=144;
return f;
}
//头标签设置,返回自定义的uiview
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
AdvertHeaderView *advertHeaderView=[[AdvertHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 144)];
return advertHeaderView;
}
这种方法,广告栏view会停留在页面上,不会随uitableview滚动如果要其随其它cell滚动,可以这样设置
self.tableViewList.tableHeaderView=advertHeaderView;
这样就大功告成,去试试吧