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);
}
但細胞高度不細胞的fixed.Height也是動態的。那麼它將不會完整填充,contion – Rokky
您的單元格中只有文本?還是其他什麼? – Gati
是的,內容僅基於文本。 – Rokky