Ios 如何应对UITableViewCell宽度的变化?
我正在开发iOS 9+应用程序。Ios 如何应对UITableViewCell宽度的变化?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在开发iOS 9+应用程序。UITableViewCell子类包含一个子视图,该子视图充当另一个“progressbar-like”视图的容器: 因此,ContainerView用于在单元格内定位ProgressView ProgressView不是真正的进度条,只是一个UIView,它的宽度应该用来表示一些进度。它使用ContainerView的顶部、底部和前导约束进行定位。宽度是使用固定值约束定义的 因此,将进度设置为某个值x仅意味着设置宽度约束的常数: // Set progress
UITableViewCell
子类包含一个子视图,该子视图充当另一个“progressbar-like”视图的容器:
因此,ContainerView
用于在单元格内定位ProgressView
ProgressView
不是真正的进度条,只是一个UIView
,它的宽度应该用来表示一些进度。它使用ContainerView
的顶部、底部和前导约束进行定位。宽度是使用固定值约束定义的
因此,将进度设置为某个值x仅意味着设置宽度约束的常数:
// Set progress to .75
ProgressCell * cell = [self.tableView dequeueReusableCellWithIdentifier:kProgressCellID];
if (cell == nil)
cell = [[ProgressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kProgressCellID];
cell.progress = 0.75;
// ProgressCell.m
- (void)setProgress:(float)value {
_progress = value;
CGFloat width = self.progressContainerView.frame.size.width * _progress;
self.progressWidthConstraint.constant = width;
}
一般来说,这很好,但在大多数情况下,它不起作用:
- 加载
,并调用UITableView
tableView:cellforrowatinexpath:
- 这些单元格将被创建或退出队列
- 已设置进度值。此时,TableView和单元格的宽度似乎是默认的320px宽度李>
- 加载页面(和所有单元格)后,TableView和所有单元格的大小将重新调整为实际设备宽度,例如375px李>
- 很明显,320%的x%比375%的x%有些不同。因此,调整大小时应更新进度约束。
布局子视图中手动重新加载进度/宽度,但这也不起作用
知道如何解决这个问题吗?即使我在使用autolayout时也遇到了同样的问题
在计算和设置进度之前,请尝试使用以下代码
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.initiativeMetricTableView.bounds), CGRectGetHeight(cell.bounds));
cell.contentView.bounds = cell.bounds;
[cell setNeedsLayout];
[cell layoutIfNeeded];
希望这会有所帮助。即使我在使用自动布局时也遇到了同样的问题
在计算和设置进度之前,请尝试使用以下代码
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.initiativeMetricTableView.bounds), CGRectGetHeight(cell.bounds));
cell.contentView.bounds = cell.bounds;
[cell setNeedsLayout];
[cell layoutIfNeeded];
希望这会有所帮助。我的建议是在单元格的帧发生更改时调用setProgressSetter(或刷新progressContainerView帧的新方法)。例如,您可以在ProgressCell子类中执行以下操作
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self setProgress:_progress];
}
我的建议是在单元格的帧发生更改时调用setProgressSetter(或刷新progressContainerView帧的新方法)。例如,您可以在ProgressCell子类中执行以下操作
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self setProgress:_progress];
}
更改进度值时重新加载tableview。并在更改进度值时将滑块值设置为table cellreload tableview。并将滑块值设置为表格单元格