0

I have UITableViewCell subview as UIScrollview and UIscrollview as dynamic uilabels and i need to scroll horizontally with pagination. but i need to scroll synchronously all the table view cell. problem is not able to scroll all the cell togeather.

here is my source code.

Customcell source:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

        //ScrollView
        self.kpiScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 70)];
        [self.kpiScrollView setPagingEnabled:YES];
        [self.contentView addSubview:self.kpiScrollView];
        [self.kpiScrollView release];                       

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

        for (int i =0; i<colors.count; i++) {
            CGRect frame;
            frame.origin.x = self.kpiScrollView.frame.size.width *i;
            frame.origin.y = 0;
            frame.size = self.kpiScrollView.frame.size;

            subView  = [[UIView alloc] initWithFrame:frame];
            subView.backgroundColor = [colors objectAtIndex:i];
            [self.kpiScrollView addSubview:subView];
            [subView release];
        }
        self.kpiScrollView.contentSize = 
            CGSizeMake(self.kpiScrollView.frame.size.width*colors.count,
                       self.kpiScrollView.frame.size.height); 
    }
}

and TableView source:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
         static NSString *cellIdentifier = @"CellIdentifier";
        PageCell *cell = (PageCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[[PageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
        }
        //    cell.pageDelegate = self;
        cell.self.kpiScrollView.delegate= self;
        cell.tag = indexPath.row+1;
        NSLog(@"cell tag:%d", cell.tag);

        return cell;
    }

UIScrollView delegate methods:

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (kpiScrollView == self.kpiTableView) {
        return;
    }
    CGPoint contentOffset = kpiScrollView.contentOffset;
    for (PageCell *cell in [self.kpiTableView visibleCells]) {
        cell.kpiScrollView.contentOffset = contentOffset;
    }
}
4

1 回答 1

1

略读代码,不确定是否还有其他问题,但首先要修复的是您获取内容偏移量的位置....

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

    // note the change here...
    if (sender == self.kpiTableView) return;

    // get the content offset from the cell's scrollview that posted this delegate message
    CGPoint contentOffset = sender.contentOffset;
    for (PageCell *cell in [self.kpiTableView visibleCells]) {
        cell.kpiScrollView.contentOffset = contentOffset;
    }
}
于 2013-04-15T05:58:18.453 回答