gpt4 book ai didi

ios - 动态单元格的高度

转载 作者:行者123 更新时间:2023-11-29 12:46:44 26 4
gpt4 key购买 nike

我看了很多关于它的教程,但还是做不好。这是代码:

@implementation DCHViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.tableViewContentArray = @[@"The franchise centers around a series of fantasy and science fantasy role-playing video games (RPGs), but includes motion pictures, anime, printed media, and other merchandise. The eponymous first game in the series, published in 1987",
@"racing, third-person shooter, fighting, and rhythm.",
@"e franchise. Recurring elements include plot themes, character names, and game mechanics. Plots center on a group of heroes battling a great evil while exploring the characters' internal struggles and relationships. Character names are frequently derived from the history, languages, and mythologies of cultures worldwide.",
@"l Fantasy has been a driving force in the video game industry, and the series has affected Square Enix's business practices and its relationships with other video game developers. It has also introduced many features now common in role-playing video games and has been credited with helping to popularize console-based RPGs in markets outside Japan.",
@" in Japan, has been bundled with Final Fantasy in several re-releases.[3][4][5] The last of the NES installments, Final Fantasy III, was release",
@"he first six games to three-dimensional (3D) computer graphics; the game features polygonal characters on pre-rendered backgrounds. It also introduced a more modern setting, a style that was carried over to the next game.[3] It was also the first in the series to be released in Europe. The eighth installment was published in 1999, and was the first to consistently use realistically proportioned characters and feature a vocal piece as its theme music.[3][13] Final Fantasy IX, released in 2000, returned to the series' roots by revisiting a more traditional Final "];
}

#pragma mark - Table View methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

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

cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
[cell.textLabel setLineBreakMode:NSLineBreakByCharWrapping];
cell.textLabel.text = self.tableViewContentArray[indexPath.row];

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont systemFontOfSize:14.0f];
gettingSizeLabel.numberOfLines = 0;
gettingSizeLabel.text = self.tableViewContentArray[indexPath.row];
[gettingSizeLabel setLineBreakMode:NSLineBreakByCharWrapping];
CGSize maximumLabelSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, CGFLOAT_MAX);

CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];


CGFloat height = expectedSize.height;

return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableViewContentArray count];
}

@end

结果我的高度不正确,并且单元格的内容(如果太长)被裁剪了。为什么会这样?

UPD:我也尝试使用 NSString 的 boundingRectWithSize 方法,具有完全相同的属性:

NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],NSStrokeColorAttributeName: [UIColor grayColor]}. 

它根本不起作用——它只有 2 行文本和非常大的宽度。看起来只是忽略了

CGSize templateSize = CGSizeMake(300.0f, CGFLOAT_MAX) that I have set.

最佳答案

已修复:

CGSize maximumLabelSize = CGSizeMake(290.0f, CGFLOAT_MAX);

CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

CGFloat height = expectedSize.height + 1;

290 因为 cell.textLabel 的宽度。但! height+1 是某种即兴创作。所以我仍然不知道为什么我应该做这个“+1”,但它有效。有谁知道这个 1 是什么意思,为什么它不包含在 sizeThatFits 返回值中?

关于ios - 动态单元格的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23510261/

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