How to connect scrollview to page control in TableView Cell

偶尔善良 提交于 2019-12-22 13:58:25

问题


I have a custom cell designed with a scrollview and a pageview control, which I am displaying as follows

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString * CellIdentifier = @"ScrollViewCell";
cell = (ScrollViewCell*)[newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    NSArray * customcellArray = [[NSBundle mainBundle]loadNibNamed:@"ScrollViewCell" owner:self options:nil];
    for(id customcellObject in customcellArray){
        if([customcellObject isKindOfClass: [UITableViewCell class]]){
            cell = (ScrollViewCell *)customcellObject;
            break;
        }
    }
}

// Customize your UIScrollView here..

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

scrollView = cell.scrollView;
pageControl = cell.pageControl;

cell.backgroundColor = [UIColor grayColor];

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...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor grayColor];
[cell setBackgroundView:bgview];

return cell;
}

The scrollview appears and scrolls just fine in cell , but problem is page control doesnt update with the scroll, basically I want to update the page control on scroll of scrollview but since page control and scrollview both are from cell I am not getting how to achieve this, I tried implementing UIScrollViewDelegate protocol with cell and then the parent view of table view but couldnt get it working, pleas guide.

Thanks Vishal


回答1:


Don't place your UIPageControll over UIScrollview in your custom cell, if it is then it will get scroll along UIScrollView itself.So create your Custom cell like this and make outlet for each UIScrollview & UIPageControll,

One change in your cellForRowAtIndexPath

// Customize your UIScrollView here..

[cell.scrollView setDelegate:self];
[cell.scrollView setPagingEnabled:YES];
[cell.scrollView setTag:indexPath.row]; // set indexpath.row as tag for cell.scrollview

NSArray * colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],[UIColor blueColor],nil];

cell.pageControll.numberOfPages = [colors count];

And in,

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

    CGFloat xOffset = sender.contentOffset.x;
    CGFloat frameWidth = sender.frame.size.width;

    int page = floor((xOffset - frameWidth / 2) / frameWidth) + 1;

    GroupButtonCell * cell = (GroupButtonCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]]; // get appropriate cell based on scrollview tag (sender.tag).
    cell.pageControll.currentPage = page; // assigning to pagecontroll

}

Hope this will help..



来源:https://stackoverflow.com/questions/15910747/how-to-connect-scrollview-to-page-control-in-tableview-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!