簡體   English   中英

在TableView Cell中使用分頁的ScrollView

[英]ScrollView with paging in TableView Cell

我必須創建一個tableview,其中一個單元格具有一個滾動視圖,其中啟用了分頁和分頁控制。 我已經嘗試過了。

我在IB中創建了一個TableView Cell,里面有兩個東西:1.ScrollView 2.Page控件和在scrollview的每個頁面上運行時放置的一些標簽,我在普通視圖中測試了這個scrollview和分頁控件,它按預期工作。

我在ScrollViewCell.h中有以下代碼

#import <UIKit/UIKit.h>

@interface ScrollViewCell : UITableViewCell<UIScrollViewDelegate>
{
UIPageControl* pageControl;
UIScrollView* scrollView;
BOOL pageControlBeingUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl* pageControl;
- (IBAction)changePage;

@end

並在ScrollViewCell.m中跟隨代碼

#import "ScrollViewCell.h"

@implementation ScrollViewCell

@synthesize scrollView;
@synthesize pageControl;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],    [UIColor blueColor], nil];
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [UIColor grayColor];

        [self.scrollView addSubview:subview];

        // add buttons
        CGRect Frame= CGRectMake(frame.origin.x,frame.origin.y,200,50);
        //set your x and y position whatever u want.

        UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
        Button.frame = Frame;

        UIImage *Img = [UIImage imageNamed:@"second.png"];
        [Button setBackgroundImage:Img forState:UIControlStateNormal];

        [scrollView addSubview: Button];

        // add text
        UILabel *label = [[UILabel alloc]    initWithFrame:CGRectMake(frame.origin.x,frame.origin.y+60,200,50)];
        label.textAlignment = UITextAlignmentCenter;
        label.text = @"Here you write your text";
        label.textColor = [UIColor blackColor];

        [scrollView addSubview:label ];
        [subview release];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);


}
 return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
    // Switch the indicator when more than 50% of the previous/next page isvisible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;

[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;

}

@end

我在表視圖中加載此單元格如下

static NSString *CellIdentifier = @"ScrollViewCell";

ScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[ScrollViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease];

}


return cell;

當我運行這個應用程序時,它顯示一個具有所需高度但沒有任何內容的空表。 我的方法是否正確? 如果是的話什么都沒有?

謝謝Vishal

您正在嘗試在UITableViewCell類中添加自定義您的UIScrollView ,我不確定它是否可行。

而是在你的UITableViewcellForRowAtIndexPath嘗試這樣的,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString * CellIdentifier = @"GroupButtonCell";
    CustomCell * cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray * customcellArray = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
        for(id customcellObject in customcellArray){
            if([customcellObject isKindOfClass: [UITableViewCell class]]){
                cell = (CustomCell *)customcellObject;
                break;
            }
        }
    }

// Customize your UIScrollView here..

    [cell.scrollView setDelegate:self];
    [cell.scrollView setPagingEnabled:YES];

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],[UIColor blueColor], nil];
    cell.scrollView.contentSize = CGSizeMake(cell.scrollView.frame.size.width * colors.count,cell.scrollView.frame.size.height);
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = cell.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = cell.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [cell.scrollView addSubview:subview];
    }
    // Configure the cell...

    return cell;
}

在ScrollViewCell.m中嘗試這個內部的init方法。

       [self.contentView addSubview:scrollView];

您應該在單元格上添加scrollview。 檢查M不確定。 我有時間嘗試項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM