如何在TableView单元格中将scrollview连接到页面控件

时间:2013-04-09 19:23:27

标签: iphone ios scrollview

我有一个设计有滚动视图和浏览量控件的自定义单元格,我显示如下

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

滚动视图出现并在单元格中滚动得很好,但问题是页面控件没有用滚动更新,基本上我想在滚动视图滚动时更新页面控件但是因为页面控件和滚动视图都来自单元格我没有得到如何实现这一点,我尝试用单元格实现UIScrollViewDelegate协议,然后是表视图的父视图,但无法使其工作,请求指南。

由于 维沙尔

1 个答案:

答案 0 :(得分:2)

请勿将UIPageControll放在自定义单元格中UIScrollview,如果是,则会自动滚动UIScrollView。因此,请创建自定义单元格,然后单独输出每个UIScrollview&amp; UIPageControll

Custom Cell

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

}

希望这会有所帮助..