【问题标题】:how to add UITableviewcell subview as uiscrollview如何将 UITableviewcell 子视图添加为 uiscrollview
【发布时间】:2013-04-15 05:20:49
【问题描述】:

我有 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;
    }
}

【问题讨论】:

  • UITableView 是 UIScrollView 的子类。所以不需要添加这个
  • @BuntyMadan,这是真的,但我认为这对他/她没有帮助。如果我正确地阅读了这个问题,他们想要一个滚动视图inside 每个单元格,这允许他们在每个单元格内水平滚动,以及使用表格视图垂直滚动。临时,如果我错了,请纠正我。
  • 同意@Nate。我看到你在这里尝试做什么,但代码中有一些真正奇怪的东西:例如cell.self.kpiScrollView.delegate=自我;如果 (kpiScrollView == self.kpiTableView)... 当然不相等。
  • cell.self 部分很奇怪。我想知道他们是否想说if (sender == self.kpiTableView) 是为了过滤掉由于表格视图(这是一个滚动视图)而导致的滚动回调与单元格内滚动视图中的滚动回调?
  • 是的。大概。我刚刚通过实验了解到 object.self == object。每天学些新东西。但是,@temp,这是不必要的打字。 cell.self.whatever 应该更改为 cell.whatever。

标签: ios objective-c uiscrollview uipagecontrol


【解决方案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;
    }
}

【讨论】:

  • +1。老实说,我什至不明白 if (kpiScrollView == self.kpiTableView) 是如何在原始代码中编译的:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多