在UITableView中,我希望在每个原型单元格中绘制一个条形图,每个单元格中的条形图具有不同的大小,以指示值的数量
对于工具栏,我使用了一个UILabel (也尝试了一个UIview,结果相同),我希望在cellForRowAtIndexPath方法中对其进行更改:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int value = //Math to calculate value per cell
LessionViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lessionItems"];
CGRect frmBlueLabel = cell.labelBlueBackground.frame;
frmBlueLabel.size.width = value;
cell.labelValue.text = [NSString stringWithFormat:@"%i", value]
cell.labelBlueBackground.frame = frmBlueLabel;
return cell;
}当然,在IB中正确地连接了cell.labelBlueBackground.frame。当我调试时,我可以看到当返回单元时cell.labelBlueBackground.frame具有正确的值。
然而,在我的应用程序中,所有单元格的大小都是相等的。
我在那个单元格中有另一个标签labelValue,我在其中设置了文本属性-它工作得很好。
在cellForRowAtIndexPath中调整大小是不可能的吗?我如何解决这个问题?
非常感谢!
发布于 2013-11-29 22:41:09
我解决了这个问题。它看起来不可能在cellForRowAtIndexPath方法期间改变帧。
经过一些研究,我能够通过以下代码解决这个问题:
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[self updateAllBars];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self updateAllBars];
}而updateAllBars是我编写的一个函数,它可以遍历所有单元格并更改框架。有趣的是,这必须在viewWillAppear和viewWillLayoutSubviews中执行
https://stackoverflow.com/questions/20237303
复制相似问题