【问题标题】:Set scrollView height dynamically动态设置滚动视图高度
【发布时间】:2013-02-02 04:53:41
【问题描述】:

我是 iphone 开发的新手,这是我的第一个问题。

我在 UIScrollView 中显示 UITableView,但我无法同时指定 tableView 和 scrollView 的高度,因为数据不是静态的,它来自服务。

我需要scrollView根据tableView的内容(高度)动态设置它的高度。 我已经这样做了,但是我看不到最后一个单元格的完整数据。

self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,327,480) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.scrollEnabled = NO;
    [testscroll addSubview:self.tableView];
    [self.tableView reloadData];
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.contentSize.height);
    float ftbl = self.tableView.frame.origin.y + self.tableView.contentSize.height + 150;
    scrollView.contentSize=CGSizeMake(320, ftbl);
    scrollView.scrollEnabled=YES;

我正在计算 tableview 的高度:

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

        UITableViewCell *cell = [self tableView: self.tableView cellForRowAtIndexPath: indexPath];

        NSString *city = lblcity.text;
        UIFont *font = [UIFont fontWithName:@"Helvetica" size:14.0];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize bounds = [city sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];


        NSString *state = lblstate.text;
        UIFont *font1 = [UIFont fontWithName:@"Helvetica" size:14.0];
        CGSize constraintSize1 = CGSizeMake(280.0f, MAXFLOAT);
        CGSize bounds1 = [state sizeWithFont:font1 constrainedToSize:constraintSize1 lineBreakMode:NSLineBreakByWordWrapping];



        NSString *name = lblname.text;
        UIFont *font2 = [UIFont fontWithName:@"Helvetica" size:14.0];
        CGSize constraintSize2 = CGSizeMake(280.0f, MAXFLOAT);
        CGSize bounds2 = [name sizeWithFont:font2 constrainedToSize:constraintSize2 lineBreakMode:NSLineBreakByWordWrapping];



        NSString *address = lbladdress.text;
        UIFont *font3 = [UIFont fontWithName:@"Helvetica" size:14.0];
        CGSize constraintSize3 = CGSizeMake(280.0f, MAXFLOAT);
        CGSize bounds3 = [address sizeWithFont:font5 constrainedToSize:constraintSize3 lineBreakMode:NSLineBreakByWordWrapping];

      return (CGFloat) cell.bounds.size.height + bounds.height+bounds1.height+bounds2.height+bounds3.height;



    }

scrollView如何根据UItableview的高度增加/减少它的高度?

编辑:

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

   static NSString *CellIdentifier=@"Cell";

        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier]autorelease];

            lblname = [[UILabel alloc] initWithFrame:CGRectZero];
            lblname.tag = 111;
            lblname.backgroundColor = [UIColor clearColor];
            [lblname setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
            [lblname setLineBreakMode:NSLineBreakByWordWrapping];
            [cell addSubview:lblname];


            lbladdress = [[UILabel alloc] initWithFrame:CGRectZero];
            lbladdress.tag = 113;
            lbladdress.backgroundColor = [UIColor clearColor];
            [lbladdress setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
            [lbladdress setLineBreakMode:NSLineBreakByWordWrapping];
            [cell addSubview: lbladdress];



           lblcity = [[UILabel alloc] initWithFrame:CGRectZero];
            lblcity.tag = 115;
            lblcity.backgroundColor = [UIColor clearColor];
            [lblcity setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
            [lblcity setLineBreakMode:NSLineBreakByWordWrapping];
            [cell addSubview: lblcity];




            lblstate=[[UILabel alloc] initWithFrame:CGRectZero];
            lblstate.tag = 116;
            lblstate.backgroundColor = [UIColor clearColor];
            [lblstate setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
            [lblstate setLineBreakMode:NSLineBreakByWordWrapping];
            [cell addSubview: lblstate];







        }

        cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
        NSMutableDictionary *d =[[NSMutableDictionary alloc]initWithDictionary: [providerarray objectAtIndex:indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSString *name2 = [d objectForKey:@"Name"];
        CGSize constraint1 = CGSizeMake(175, 2000.0f);
        CGSize size1 = [name2 sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
        lblname = (UILabel *)[cell viewWithTag:111];
        lblname.text = [NSString stringWithFormat:@"%@",name2];
        [lblname setNumberOfLines:0];
        lblname.frame = CGRectMake(105,25, size1.width, size1.height);



            NSString *lane2 = [d  objectForKey:@"Address"];
            CGSize constraint3 = CGSizeMake(175, 2000.0f);
            CGSize size3 = [line2 sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint3 lineBreakMode:NSLineBreakByWordWrapping];
            lbladdress = (UILabel *)[cell viewWithTag:113];
            lbladdress.text = [NSString stringWithFormat:@"%@",lane2];
            [lbladdress setNumberOfLines:0];
            lbladdress.frame = CGRectMake(105,lblname.frame.size.height+lblname.frame.origin.y, size3.width, size3.height);



                NSString *city2 =[d  objectForKey:@"City"];


                NSString *trimmedString = [city2 stringByTrimmingCharactersInSet:
                                           [NSCharacterSet whitespaceAndNewlineCharacterSet]];
                NSLog(@"%@", trimmedString);
                CGSize constraint5 = CGSizeMake(175, 2000.0f);
                CGSize size5 = [trimmedString sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint5 lineBreakMode:NSLineBreakByWordWrapping];
                lblcity = (UILabel *)[cell viewWithTag:115];
                lblcity.text = [NSString stringWithFormat:@"%@",trimmedString];
                [lblcity setNumberOfLines:0];
                lblcity.frame = CGRectMake(105,lbladdress.frame.size.height+lbladdress.frame.origin.y, 175, size5.height);


        NSString *state1=[d objectForKey:@"State"];

        CGSize constraint6=CGSizeMake(175,2000.0f);
        CGSize size6=[state1 sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]  constrainedToSize:constraint6 lineBreakMode:NSLineBreakByWordWrapping];
        state=(UILabel *)[cell viewWithTag:116];
        [state setText:[NSString stringWithFormat:@"%@",state1]];


        state.frame=CGRectMake(105, lblcity.frame.size.height+lblcity.frame.origin.y, size6.width, size6.height);
        [state setTextAlignment:NSTextAlignmentLeft];
        [state sizeToFit];



           return cell;
    }

【问题讨论】:

  • 嘿,你为什么要在scrollview中使用tableview?当你向上或向下滚动时,Tableview本身会滚动......你能详细说明你的问题吗?
  • 但我不想为tableview设置滚动。是的tableView本身有滚动。但是如果数据很大我不知道tableView在一个视图中支持多少个单元格以及如果单元格该怎么办有很多(如何动态设置高度)?
  • 但是不管有多少数据tableview内容,它都会自动创建tableview的高度。如果你说10个数据那么tableview高度就足以显示10个数据,如果你有100个数据那么tableview的高度也足以显示100条数据,你需要设置的是它的numberOfRowsInSection方法和cellForRowAtIndexPath方法。
  • 我也这样做了。但它显示的单元格仅适合视图高度,即 480。除此之外它不显示。我还需要做什么?
  • 好的,请发布您的 cellForRowAtIndexPath 方法的代码...

标签: iphone uitableview uiscrollview xcode4.5


【解决方案1】:

我会观察表格视图的 contentSize 属性,并据此调整滚动视图的高度。例如:

假设你有你的表格视图。你可以像这样观察它的内容大小:

[self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];

现在,您可以将此方法添加到您的视图控制器:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {



}

在此方法中,您可以添加一些逻辑来查看观察值发生了哪些变化,然后调整滚动视图的高度。

if (object == self.tableView) {

    if ([keyPath isEqualToString:@"contentSize"]) {

        CGRect scrollViewFrame = self.scrollView.frame;
        scrollViewFrame.height = self.tableView.contentSize.height;
        [self.scrollView setFrame:scrollViewFrame];

    }

}

终于可以在dealloc中移除观察者了。

-(void)dealloc {

    [self.tableView removeObserver:self forKeyPath:@"contentSize"];

}

【讨论】:

  • 但它没有进入这个 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 我该怎么做需要调用它吗?
猜你喜欢
  • 2020-01-30
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
相关资源
最近更新 更多