0

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

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

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

  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];

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

非常感谢 !

4

1 回答 1

2

添加所有控件(如标签、滚动视图等cell.contentView而不是cell addSubview.

于 2013-04-06T19:00:44.203 回答