【发布时间】:2014-06-23 23:38:59
【问题描述】:
我阅读了很多关于它的教程,但仍然无法正确处理。代码如下:
@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.
【问题讨论】:
标签: ios dynamic uitableview height