简体   繁体   中英

UIScrollView inside a UITableViewCell

This code shows how to add a scrollview to a cell and works perfect but I want to load a scrollview which I have designed in XIB and which is paging enabled, I tried directly loading the scrollview as a contentview to cell but it doest appear proper and doesnt scroll horizontally which it does outside the tableview cell, please guide me what changes will be required

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

I am adding some other UI components to my XIB scrollview at runtime like button etc. code is as below

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

Please suggest me the modifications with which i can load my designed scrollview to tableview cell

Thanks a lot !

cell.contentView添加所有控件( 如label,scrollView等 ),而不是cell addSubview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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