gpt4 book ai didi

ios - 如何使UITableView高度动态变化到特定高度,然后在不使用ios中使用AutolayOut的情况下进行滚动

转载 作者:行者123 更新时间:2023-12-01 20:04:14 29 4
gpt4 key购买 nike

我想使tableview的高度动态(基于加载的单元格的数量)达到特定限制。达到该限制后,我想使UITableView可滚动。我不想为此使用自动布局。请帮我。提前致谢。

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

}

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

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

最佳答案

您可以使用以下代码,

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和行的高度。

希望它能对您有所帮助!
干杯

关于ios - 如何使UITableView高度动态变化到特定高度,然后在不使用ios中使用AutolayOut的情况下进行滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39326844/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com