如何将scrollview连接到TableView Cell中的页面控件 [英] How to connect scrollview to page control in TableView Cell

查看:51
本文介绍了如何将scrollview连接到TableView Cell中的页面控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设计为带有滚动视图和页面视图控件的自定义单元格,我将其显示如下

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;
}

scrollview出现并在单元格中滚动得很好,但是问题是页面控件没有随滚动而更新,基本上我想在scrollview的滚动时更新页面控件,但是由于页面控件和scrollview都来自单元格,所以我没有得到如何实现此目的,我尝试使用cell然后使用table view的父视图实现UIScrollViewDelegate协议,但无法正常工作,请指导.

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.

谢谢 维沙尔

推荐答案

不要将UIPageControll放在自定义单元格中的UIScrollview上,如果是,它将沿UIScrollView本身滚动.这样创建您的自定义单元格,并为每个UIScrollview& UIPageControll

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,

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

}

希望这会有所帮助.

这篇关于如何将scrollview连接到TableView Cell中的页面控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆