【问题标题】:UIScrollView inside a UITableViewCellUITableViewCell 内的 UIScrollView
【发布时间】:2013-03-29 02:13:45
【问题描述】:

这段代码显示了如何将滚动视图添加到单元格并且工作完美,但我想加载我在 XIB 中设计的滚动视图并且启用了分页,我尝试直接将滚动视图作为内容视图加载到单元格但它确实没有显示正确并且不会像在表格视图单元格之外那样水平滚动,请指导我需要进行哪些更改

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"ScrollCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 25, 320, 25)];
    scroller.showsHorizontalScrollIndicator = NO;

    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
    contentLabel.backgroundColor = [UIColor blackColor];
    contentLabel.textColor = [UIColor whiteColor];
    NSMutableString *str = [[NSMutableString alloc] init];
    for (NSUInteger i = 0; i < 100; i++) { [str appendFormat:@"%i ", i]; }
    contentLabel.text = str;

    [scroller addSubview:contentLabel];
    scroller.contentSize = contentLabel.frame.size;
    [cell addSubview:scroller];
}

// cell.textLabel.text = [NSString stringWithFormat:@"cell #%i", indexPath.row];

return cell;
}

我在运行时向我的 XIB 滚动视图添加了一些其他 UI 组件,例如按钮等。代码如下

  NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIView *subview = [[UIView alloc] initWithFrame:frame];
    subview.backgroundColor = [UIColor grayColor];

    [self.scrollView addSubview:subview];

    // add buttons
    CGRect Frame= CGRectMake(frame.origin.x,frame.origin.y,200,50);
    //set your x and y position whatever u want.

    UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
    Button.frame = Frame;

    UIImage *Img = [UIImage imageNamed:@"second.png"];
    [Button setBackgroundImage:Img forState:UIControlStateNormal];

    [scrollView addSubview: Button];

    // add text
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x,frame.origin.y+60,200,50)];
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"Here you write your text";
    label.textColor = [UIColor blackColor];

    [scrollView addSubview:label ];


    [subview release];

请建议我可以将设计的滚动视图加载到表格视图单元格的修改

非常感谢!

【问题讨论】:

  • 如果有更好的方法来实现我正在做的事情,请提出建议!
  • OK 同时我做了一些更改并使用了自定义表格视图单元格方法,我现在正在加载自定义单元格但滚动视图再次不可见,我在 initWithStyle 和组件中添加了相同的滚动视图代码到自定义表格视图使用 IB 的单元格我觉得这是更清洁的方法,但包括滚动视图在内的任何组件都不会加载到单元格中。

标签: iphone ios objective-c


【解决方案1】:

cell.contentView 而不是cell addSubview 中添加所有控件(例如标签、scrollView 等。)。

【讨论】:

  • 但我想保留 Scrollview 的分页,并且这些控件在 scrollview 页面中移动,将它们添加到单元格将失去该功能。
猜你喜欢
  • 2011-05-18
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多