0

UITableViewCell サブビューを UIScrollview として、UIscrollview を動的 uilabels として持っており、ページネーションで水平方向にスクロールする必要があります。しかし、すべてのテーブルビューセルを同期的にスクロールする必要があります。問題は、すべてのセルを一緒にスクロールできません。

これが私のソースコードです。

カスタムセル ソース:

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

およびTableViewソース:

    - (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 デリゲート メソッド:

- (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 に答える