page control using scrollview

vc.h

#import <UIKit/UIKit.h>

@interface scrollingViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *sv;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;


@end

vc.m

#import "scrollingViewController.h"
#import "scrollingTableViewCell.h"
@interface scrollingViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>{
    NSArray *arr,*arr1,*arr2,*arr3;
    CGFloat screenWidth,screenHeight;
    NSInteger tableCount;
    UITableView *v;
    NSMutableArray *myArray,*ary;
    NSInteger vcount;
    UITableView *table;
}

@end

@implementation scrollingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myArray=[[NSMutableArray alloc]init];
    tableCount=4;
    _pageControl.numberOfPages=tableCount;
    [self createElements];
    
    
    
}


-(void)createElements
{
    
    arr=[NSArray arrayWithObjects:@"hi",@"h",@"h",@"h", nil];
    arr1=[NSArray arrayWithObjects:@"hi",@"hi",@"hi",@"hi", nil];
    arr2=[NSArray arrayWithObjects:@"hi",@"hk",@"hj",@"ho", nil];
    arr3=[NSArray arrayWithObjects:@"hi",@"hk",@"hj",@"ho", nil];
    ary = [[NSMutableArray alloc]initWithObjects:arr,arr1,arr2,arr3, nil];
    NSLog(@"%@",ary);
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    screenWidth = screenRect.size.width;
    screenHeight = screenRect.size.height;
    if (tableCount>0) {
        
        for (int i=0; i<tableCount; i++) {
            v=[[UITableView alloc]initWithFrame:CGRectMake(i*screenWidth, 0,screenWidth,screenHeight)];
            [myArray addObject:v];
            
        }
    }
    
//    UITableView *v=[[UITableView alloc]initWithFrame:CGRectMake(0, 0,screenWidth,screenHeight)];
//    UITableView *v1=[[UITableView alloc]initWithFrame:CGRectMake(screenWidth, 0, screenWidth,screenHeight)];
//    UITableView *v2=[[UITableView alloc]initWithFrame:CGRectMake(2*screenWidth, 0, screenWidth,screenHeight)];
    _sv.showsHorizontalScrollIndicator=NO;
    _sv.showsVerticalScrollIndicator=NO;
    
    for (int i=0; i<myArray.count; i++) {
        
        table=[myArray objectAtIndex:i];
//make scroll view tag to high value(eg. 10000)
       table.tag=i;
        [_sv addSubview:table];
        table.delegate=self;
        table.dataSource=self;
    }
    
    
//    [_sv addSubview:v1];
//    [_sv addSubview:v2];
    
    _sv.delegate=self;
    _sv.contentSize=CGSizeMake(self.sv.frame.size.width*tableCount, self.sv.frame.size.height);
    
//    v.delegate=self;
//    v.dataSource=self;
    


}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray* val;
    val=ary[_pageControl.currentPage];
    vcount=val.count;
    NSLog(@"%lu",(unsigned long)val.count);

    return vcount;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    scrollingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"scrollingTableViewCell"];
    if (cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"scrollingTableViewCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }
    NSLog(@"%ld",(long)_pageControl.currentPage);
    
    cell.l.text=[ary[_pageControl.currentPage] objectAtIndex:indexPath.row ];
    NSLog(@"%@",cell.l.text);
    return cell;
 }


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    CGFloat pageWidth = screenWidth;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  
    if ((targetContentOffset->x - (floor(targetContentOffset->x / pageWidth) * pageWidth)) > pageWidth) {
        page++;
    }
    targetContentOffset->x = page * pageWidth;
    _pageControl.currentPage=page;
   UITableView *tempTable=(UITableView *)[_sv viewWithTag:_pageControl.currentPage];

    [tempTable reloadData];
    
}


@end
Previous
Next Post »

1 comments:

Write comments