如何将UITableviewcell子视图添加为uiscrollview

时间:2013-04-15 05:20:50

标签: ios objective-c uiscrollview uipagecontrol

我将UITcrollview和UIscrollview作为动态uilabels的UITableViewCell子视图,我需要使用分页水平滚动。但我需要同步滚动所有表视图单元格。问题是无法滚动所有细胞togeather。

这是我的源代码。

Customcell来源:

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

1 个答案:

答案 0 :(得分: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;
    }
}
相关问题