简体   繁体   English

如何将UITableviewcell子视图添加为uiscrollview

[英]how to add UITableviewcell subview as uiscrollview

I have UITableViewCell subview as UIScrollview and UIscrollview as dynamic uilabels and i need to scroll horizontally with pagination. 我有UITableViewCell子视图作为UIScrollview和UIscrollview作为动态uilabels,我需要通过分页水平滚动。 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: 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); 
    }
}

and TableView source: 和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 delegate methods: 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;
    }
}

Skimming the code, not sure if there are other problems, but the first thing to fix is where you get the content offset.... 浏览代码,不确定是否还有其他问题,但是要解决的第一件事是在哪里获得内容偏移。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM