Mof*_*Mof 6 objective-c uitableview ios ios-autolayout uitableviewautomaticdimension
我有一个自定义表格视图单元格,它使用自动布局,并有一个披露指标作为附件视图.首次显示时,屏幕上单元格的单元格大小完全错误:
正如您所看到的那样,该单元占用了大约1.5个屏幕的空间:
但是,如果我旋转设备并向后旋转它看起来很好:
正如你在这里看到的,我没有做过任何复杂的事情:
我有一个非常非理想的解决方法:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
但是当你第一次看到屏幕时,这显然会导致"闪光".在更复杂的情况下,闪光灯更加明显.
我有另一个解决方法,但这会导致自动布局异常:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
cell.basicLabel.text = @"Hello this is just some text that should get the label to go over multiple lines";
[cell.basicLabel layoutIfNeeded];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
例外:
至少这种方法不会给我UI闪烁.
如果我删除附件视图,它实际上完全正常.
更新:我已经向github添加了一个示例项目:https: //github.com/fwaddle/TableCellAccessoryTest
更新#2:关于这个错误的另一项工作是在代码中布局单元格.我只是尝试在代码中做同样的事情,它没有抛出警告并且工作正常.看起来像一个IB bug.
有任何想法如何解决这个问题?谢谢.
因此,即使在代码中创建约束有时也无法解决此问题。看来你还需要一些改变。在自定义表格单元格中添加以下内容,特别是如果您根据单元格的内容更改配件类型(例如复选标记):
-(void) setAccessoryType:(UITableViewCellAccessoryType)accessoryType {
[super setAccessoryType:accessoryType];
[self setNeedsUpdateConstraints];
}
Run Code Online (Sandbox Code Playgroud)
还要删除故事板中的原型单元并注册您的类:
-(void) viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[MyCustomCell class] forCellReuseIdentifier:@"MyCustomCell"];
}
Run Code Online (Sandbox Code Playgroud)
我偶尔发现我仍然需要强制标签(尤其是看起来的多行标签)在 cellForRowAtIndexPath 中重新布局:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell" forIndexPath:indexPath];
cell.customLabel.text = .....
[cell.customLabel layoutIfNeeded];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
以上所有内容都解决了我的问题,因此我希望它们对其他人有用,并且您不要在这方面浪费大量时间。
我仍然没有收到苹果关于该错误报告的任何回复,但我认为这很正常。
归档时间: |
|
查看次数: |
3606 次 |
最近记录: |