带有 UIImage 的 UITableViewCell,初始显示单元格的宽度不更新 [英] UITableViewCell with UIImage, width not updating on initial displayed cells
问题描述
我想动态调整 UITableViewCell 内 UIImage 的宽度,我正在使用故事板设计 UITableViewCell,我刚刚添加了一个标签和一个图像,属性得到正确更新,我什至正在加载标签中的宽度值以显示它是正确的值,对于图像,我正在加载我想重复的背景图像,但如果我向上和向下滚动,图像最初不会更新宽度, 图像按预期显示,这里是 cellForRowAtIndexPath 的代码,我也尝试将代码放在 willDisplayCell 方法上,结果相同
I would like to dynamically adjust the width of a UIImage inside of a UITableViewCell, I'm using the storyboard to design the UITableViewCell, I just added a label and an image, the properties get updated correctly, I'm even loading the value of the width into the label to show that it's the correct value, for the image, I'm loading a background image that I want to repeat, but the image won't update the width initially, if I scroll up and down, the images are shown as expected, here's the code for the cellForRowAtIndexPath, I've also tried to put the code on the willDisplayCell method, same result
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycustomcell"];
int r = [[data objectAtIndex:indexPath.row] intValue];
UIImageView *img = (UIImageView *)[cell viewWithTag:2];
img.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"some_img" ofType:@"png"]]];
CGRect frame = img.frame;
frame.size.width = r*16;
img.frame = frame;
int n = img.frame.size.width;
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [NSString stringWithFormat:@"custom %d", n];
[cell setNeedsDisplay];
return cell;
}
我只是希望它在滚动后可以正常工作,想法?
I just want this to work initially as it works after scrolling, thoughts?
推荐答案
啊,移除自动布局解决了这个问题
argh, removing Auto Layout fixed the problem
这篇关于带有 UIImage 的 UITableViewCell,初始显示单元格的宽度不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!