2

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

- (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 协议,然后是表格视图的父视图,但无法使其正常工作,请指导。

谢谢维沙尔

4

1 回答 1

3

不要将你的UIPageControlloverUIScrollview放在你的自定义单元格中,如果是的话,它会自行滚动。所以UIScrollView像这样创建你的自定义单元格并为每个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

}

希望这会有所帮助..

于 2013-04-10T04:20:59.103 回答