2013-02-02 174 views
0

我是iphone开发的新手,这是我的第一个问题。动态设置scrollView高度

我在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; 



    } 

如何滚动视图增加/根据UITableView的高度降低其高度?

编辑:

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

    static NSString *[email protected]"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; 
    } 
+0

嘿,你为什么要在scrollview中使用tableview?当你向上或向下滚动时,Tableview本身会滚动...你能否详细说明你的问题,我的朋友? – NiravPatel

+0

但我不想为tableview.yes设置滚动tableView本身有scroll.But如果数据很大,我不知道有多少单元格tableview支持视图和如果单元格很多(如何设置高度动态)? –

+0

但无论多少数据tableview内容,它都会自动创建tableview的高度。如果您有10个数据,那么tableview高度就足以显示10个数据,如果您有100个数据,那么tableview高度也足够显示100个数据,你必须设置它的numberOfRowsInSection方法和cellForRowAtIndexPath方法。 – NiravPatel

回答

0

我会观察表视图的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"]; 

} 
+0

但它没有进入 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {我如何需要打电话给它? –