2016-09-05 49 views
1

我想使tableview高度動態變化(基於加載的單元格數)達到特定限制。並在達到該限制後,我想讓UITableView滾動。我不想爲此使用自動佈局。請幫幫我。提前致謝。如何將UITableView高度動態調整到特定高度,然後允許在不使用AutolayOut的情況下滾動ios

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrScreenTime.count; 

} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellIdentifier [email protected]"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

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

    cell.textLabel.text = [arrScreenTime objectAtIndex:indexPath.row]; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 

    cell.textLabel.font = [UIFont fontWithName:kRobotoRegular size:25]; 

    cell.textLabel.textColor = kColor(2, 109, 150, 1); 

    UIView *customColorView = [[UIView alloc] init]; 

    customColorView.backgroundColor = kColor(53, 144, 177, 1); 
    cell.selectedBackgroundView = customColorView; 

    cell.textLabel.highlightedTextColor = kColor(255, 255, 255, 1); 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellText = [arrScreenTime objectAtIndex:indexPath.row]; 
    UIFont *cellFont = [UIFont fontWithName:kRobotoRegular size:25]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 

    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; 

    NSLog(@"height=%f",labelSize.height); 
    return labelSize.height + 8; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

} 

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     CGFloat tableHeight = 0.0f; 

     float baseHeightofTableView = tblViewTitle.frame.size.height; 

    NSLog(@"baseHeightofTableView = %f",baseHeightofTableView); 


     for (int i = 0; i < [arrScreenTime count]; i++) { 

      tableHeight += [self tableView:tblViewTitle heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     } 

     tblViewTitle.frame = CGRectMake(tblViewTitle.frame.origin.x, tblViewTitle.frame.origin.y, tblViewTitle.frame.size.width, tableHeight); 

     NSLog(@"tblViewTitle.frame = %@",NSStringFromCGRect(tblViewTitle.frame)); 

     tblViewTitle.center = CGPointMake(self.frame.size.width/2, 
              self.frame.size.height/2); 
    } 

回答

1

我已經implimented此代碼段和它工作正常,我..

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrBasics.count; 

} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellIdentifier [email protected]"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

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

    cell.textLabel.text = [arrBasics objectAtIndex:indexPath.row]; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 

    cell.textLabel.font = [UIFont fontWithName:kRobotoRegular size:25]; 

    cell.textLabel.textColor = kColor(2, 109, 150, 1); 

    UIView *customColorView = [[UIView alloc] init]; 

    customColorView.backgroundColor = kColor(53, 144, 177, 1); 
    cell.selectedBackgroundView = customColorView; 

    cell.textLabel.highlightedTextColor = kColor(255, 255, 255, 1); 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellText = [arrBasics objectAtIndex:indexPath.row]; 
    UIFont *cellFont = [UIFont fontWithName:kRobotoRegular size:25]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 

    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; 

    NSLog(@"height=%f",labelSize.height); 
    return labelSize.height + 8; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

} 

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     CGFloat tableHeight = 0.0f; 

     float baseHeightofTableView = tblViewTitle.frame.size.height; 

     for (int i = 0; i < [arrBasics count]; i++) 
     { 
      if (tableHeight < baseHeightofTableView) { 

       tableHeight += [self tableView:tblViewTitle heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
      } 
      else{ 
       tableHeight = baseHeightofTableView; 

       break; 
      } 
     } 

     tblViewTitle.frame = CGRectMake(tblViewTitle.frame.origin.x, tblViewTitle.frame.origin.y, tblViewTitle.frame.size.width, tableHeight); 

     NSLog(@"tblViewTitle.frame = %@",NSStringFromCGRect(tblViewTitle.frame)); 

     tblViewTitle.center = CGPointMake(self.frame.size.width/2, 
              self.frame.size.height/2); 

} 
1

您可以使用下面的代碼,

if ([rowCount] > 5) { // 5 is the specific limit to which you wanted to increase table height and after that it goes in else part to not increase the height of tableview instead it will make tableview scrollable. 
    tableview.frame = CGRectMake(0,0, self.tableview.frame.size.width, 5 * [height of row]); 
} else { 
    tableview.frame = CGRectMake(0, 0, self.tableview.frame.size.width, [rowCount] * 50); 
} 

更換rowCount時,並根據您的需要行的高度。

希望它能幫助你! 乾杯

+0

但細胞高度不細胞的fixed.Height也是動態的。那麼它將不會完整填充,contion – Rokky

+0

您的單元格中只有文本?還是其他什麼? – Gati

+0

是的,內容僅基於文本。 – Rokky