2014-01-30 49 views
0

我想創建類似於這樣的東西hereUIScrollView與UITableViewCell中的UIPageControl

這裏是我試圖示例代碼:

- (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath { 靜態的NSString * simpleTableIdentifier = @ 「RefundMeCell」 ;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    cell.selectionStyle=UITableViewCellSelectionStyleNone; 
} 

UIImageView *rowImageView=(UIImageView*)[cell viewWithTag:1]; 
UILabel *questionOptionLabel=(UILabel*)[cell viewWithTag:2]; 
UIImageView *collapseImageView=(UIImageView*)[cell viewWithTag:3]; 
UIScrollView *scrollView=(UIScrollView*)[cell viewWithTag:4]; 
UIImageView *sampleImageView=(UIImageView*)[cell viewWithTag:5]; 
UIButton *sampleButton=(UIButton*)[cell viewWithTag:6]; 
UILabel *instructionHeaderLabel=(UILabel*)[cell viewWithTag:7]; 
UILabel *instructionsLabel=(UILabel*)[cell viewWithTag:8]; 
UIPageControl *pgCntrl=(UIPageControl*)[cell viewWithTag:9]; 

[rowImageView setImage:[UIImage imageNamed:@"normalcell.png"]]; 
questionOptionLabel.text = @""; 
collapseImageView.image = nil; 
scrollView.hidden = YES; 
sampleImageView.image = nil;sampleImageView.hidden = YES; 
sampleButton.hidden = YES; 
instructionHeaderLabel.text = @"";instructionHeaderLabel.hidden = YES; 
instructionsLabel.text = @"";instructionsLabel.hidden = YES; 
pgCntrl.hidden = YES; 


[sampleButton addTarget:self action:@selector(userImageButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 


sampleImageView.image = nil; 

[collapseImageView setImage:[UIImage imageNamed:@"down.png"]]; 
[email protected]""; 
[email protected]"Instructions"; 

if (indexPath.row == 0) 
{ 
    instructionHeaderLabel.hidden = NO; 
    sampleImageView.image = self.barcodeImage; 
    scrollView.hidden = NO; 
    scrollView.delegate = self; 
    pgCntrl.hidden = NO; 
    sampleImageView.hidden = NO; 
    sampleButton.hidden = NO; 
    pgCntrl.numberOfPages = 5; 
    pgCntrl.currentPage = 0; 
    for (int i = 0; i<5; i++) 
    { 
     [scrollView addSubview:sampleImageView]; 
     [scrollView addSubview:sampleButton]; 
    } 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 5, scrollView.frame.size.height); 
} 
else 
{ 
    instructionHeaderLabel.hidden = NO; 
    sampleImageView.image = nil; 
    scrollView.hidden = NO; 
    scrollView.delegate = self; 
    pgCntrl.hidden = NO; 
    sampleImageView.hidden = NO; 
    sampleButton.hidden = NO; 
    pgCntrl.numberOfPages = 3; 
    pgCntrl.currentPage = 0; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3, scrollView.frame.size.height); 
} 

return cell; 

}

我的問題是,如何添加UIImageViewUILabel,當UIScrollView被滾動到第二頁等?

它應該是這樣的

http://oi60.tinypic.com/97snib.jpg

目前UIImageViewUILabel只顯示第一次。當我滾動到第二頁時,UIImageViewUILabel向後滾動,第二頁上不顯示任何內容。

+0

爲什麼你需要添加圖片滾動時?你可以馬上添加它,滾動只會顯示它們嗎? –

+0

@圖片,您可以上傳圖像到網站,如tinypic,並分享鏈接。 @第二頁,你想延遲加載數據?如果這是你想要的,你可能會找到一些開源庫。 :) – Sufian

+0

@OlegFedorov我需要從不同的維度(頂部,底部等)產品圖像和標籤提供說明。 – Maverick

回答

0

我會建議你使用不同的方法,因爲你的解決方案是不可擴展的。如果你想要n個頁面,你必須在你的滾動視圖中添加n個圖像&標籤,這將是一團糟,也不是高效的內存。

看看this library我認爲它會適合您的需求。

+0

有點非常類似我正在尋找... – Maverick

1

這裏是我的代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    tableObj.dataSource = self; 
    tableObj.delegate = self; 
    scrollView.delegate = self; 
    // customObj = [[customCellForText alloc]init]; 
    imageArray = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"image1.jpg"], 
    [UIImage imageNamed:@"image2.jpg"], 
    [UIImage imageNamed:@"man-actor-Ashton-Kutcher-Wallpaper.jpg"], 
    [UIImage imageNamed:@"Man-Booker-Prize-judges-p-011.jpg"], 
    [UIImage imageNamed:@"man-curl.jpg"] 
    ,nil]; 

    // pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(170, 180, 100, 30)]; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 5; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *cellIdentifier = @"cell"; 
    customCellForText *cell = [tableObj dequeueReusableCellWithIdentifier:cellIdentifier]; 
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"customCellForText" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 

    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(130, 80, 150, 100)]; 
    scrollView.pagingEnabled = YES; 
    scrollView.delegate = self; 
    [scrollView setShowsHorizontalScrollIndicator:NO]; 
    //[scrollView setShowsVerticalScrollIndicator:NO]; 
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height); 
    [cell.contentView addSubview:scrollView]; 

    for (int i = 0; i < imageArray.count; i++){ 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 

     subview = [[UIImageView alloc] initWithFrame:frame]; 
     subview.image = [imageArray objectAtIndex:i]; 
     subview.contentMode = UIViewContentModeScaleAspectFit; 
     [self.scrollView addSubview:subview]; 

    } 

    pageControl = [[UIPageControl alloc]init]; 
    pageControl.frame = CGRectMake(170, 180, 100, 30); 
    pageControl.numberOfPages = imageArray.count; 
    pageControl.currentPage = 0; 
    pageControl.hidesForSinglePage = YES; 
    [cell.contentView addSubview:pageControl]; 
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; 

    [email protected]"amol"; 

    return cell; 
} 

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

    CGFloat pageWidth = sender.frame.size.width; 
    int newPage = floor((sender.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

    self.pageControl.currentPage = newPage; 

    } 
相關問題