three20 如何在UITableVIew 上面加一个可以滑动的广告条

 

效果类似于这样

 

最近一直比较纠结如何做好一个app,前段时间开始学习three20, three20确定可以简化很多问题,上面类似的效果很多app上面都有用到过,如何做呢? 我尝过多次, 思路两种:

1. 上面一个ttscrollview+ ttpagecontrol,下面一个tttableview.但是你发会现上面一直是固定的,下面uitableview动上面的不动。

2. tttbaleview 中来处理,利用tableHeaderView中做,代码如下:

#import "TSHomeViewController.h"
#import "TSHomeDataSource.h"

@interface TSHomeViewController ()

@end

@implementation TSHomeViewController


- (void)dealloc {
    _scrollView.delegate = nil;
    _scrollView.dataSource = nil;
    TT_RELEASE_SAFELY(_scrollView);
    TT_RELEASE_SAFELY(_pageControl);
    TT_RELEASE_SAFELY(_images);
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Home";
        NSString *imgPath = [NSString stringWithFormat:@"bundle://%@.png",self.title];
        self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:self.title image:TTIMAGE(imgPath) tag:0] autorelease];
        
        NSString *text =@"http://v2.expatree.com/uploadfile/2012/0419/94bc8f2fbca0df76785a1cfbd0deb651.png,http://v2.expatree.com/images/api/lion.png";
        
        _images =[[text componentsSeparatedByString:@","] retain];
        
        self.tableViewStyle = UITableViewStyleGrouped;
        self.variableHeightRows = YES;
    }
    return self;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)createModel {

    self.dataSource = [[[TSHomeDataSource alloc]
                        initWithSearchQuery:@"v2"] autorelease];}

- (void)loadView {
    [super loadView];
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.frame.size.width, 80.0f)];

    
    _scrollView = [[TTScrollView alloc] initWithFrame:CGRectMake(0,0, 320, 80 )];
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    _scrollView.dataSource = self;
    _scrollView.delegate = self;
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.zoomEnabled = NO;
    //    [_scrollView zoomToFit];
    [self.tableView.tableHeaderView addSubview:_scrollView];
    
    _pageControl = [[TTPageControl alloc] initWithFrame:CGRectMake(0,80-20, 320, 20)];
    _pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _pageControl.backgroundColor = [UIColor clearColor];
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = [_images count];
    [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self.tableView.tableHeaderView addSubview:_pageControl];
    
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTScrollViewDataSource

- (NSInteger)numberOfPagesInScrollView:(TTScrollView*)scrollView {
    return _images.count;
}

- (UIView*)scrollView:(TTScrollView*)scrollView pageAtIndex:(NSInteger)pageIndex {
    TTView* pageView = nil;
    if (!pageView) {
        pageView = [[[TTView alloc] init] autorelease];
        pageView.backgroundColor = [UIColor redColor];
        pageView.userInteractionEnabled = NO;
        pageView.frame = CGRectMake(0,0, self.view.bounds.size.width, 80);
        pageView.contentMode = UIViewContentModeLeft;
    }
    
    
    TTImageView *imgView = [[TTImageView alloc]initWithFrame:CGRectZero];
    [[TTURLRequestQueue mainQueue] setMaxContentLength:0];
    imgView.urlPath= [_images objectAtIndex:pageIndex];
    imgView.autoresizesToImage = YES;
    [pageView addSubview:imgView];
    return [pageView autorelease];
}

- (CGSize)scrollView:(TTScrollView*)scrollView sizeOfPageAtIndex:(NSInteger)pageIndex {
    return CGSizeMake(320, 80);
}

#pragma mark -
#pragma mark TTScrollViewDelegate

- (void)scrollView:(TTScrollView*)scrollView didMoveToPageAtIndex:(NSInteger)pageIndex {
    _pageControl.currentPage = pageIndex;
}

#pragma mark -
#pragma mark UIViewController overrides
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}

#pragma mark -
#pragma mark TTPageControl

- (IBAction)changePage:(id)sender {
    int page = _pageControl.currentPage;
    [_scrollView setCenterPageIndex:page];
}

 

最后:转载请注明文章出处。http://www.cnblogs.com/hubj/archive/2012/05/18/2507395.html

posted @ 2012-05-18 10:50  alex hu  阅读(2124)  评论(0编辑  收藏  举报