这段代码显示了如何向单元格中添加滚动视图并能完美地工作,但是我想加载我在XIB中设计的并且启用了分页的滚动视图,我尝试直接将滚动视图作为contentview加载到单元格,但是它看起来不正确,并且不会在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];


请提出修改建议,以便将设计的滚动视图加载到表格单元格中

非常感谢 !

最佳答案

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

关于iphone - UITableViewCell内的UIScrollView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15854670/

10-10 18:23
查看更多