目前我有一个UITableView
调整大小UITextView
.单元格使用beginUpdates
/ 自动调整大小endUpdates
,但是当它执行时,表格视图会断断续续(请参阅下面的gif).
最终结果是在UITableViewCell
其中具有基于其内容调整大小的文本视图.以下是自定义UITableViewCell
类中导致UITableView
更新自身的代码.
- (void)textViewDidChange:(UITextView *)textView {
// This is a category on UITableViewCell to get the [self superView] as the UITableView
UITableView *tableView = [self tableView];
if (tableView){
[tableView beginUpdates];
[tableView endUpdates];
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我已经尝试过的事情:
获取当前contentOffset
并重置它endUpdates
但不起作用UITableView
在更新之前禁用滚动然后启用我尝试NO
从- (BOOL)textViewShouldEndEditing:(UITextView *)textView
我的UITableView
单元格高度返回总是使用UITableViewAutomaticDimension
.欢迎任何其他想法或想法.
以下是它的样子:
我不打算使用任何库,所以请不要提出任何建议.
谢谢
编辑:解决方案
在这里找到:我不希望动画在开始更新,结束更新块为uitableview?
感谢@JeffBowen获得了一个很好的发现(虽然hacky它是可行的并且允许我仍然实现UITableViewDelegate
支持iOS 7 的方法).在执行更新之前关闭动画,然后在更新后启用以防止UITableView
口吃.
[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)
如果您不需要使用Delegate方法并且只想要一个不那么hacky的iOS 8+解决方案,那么请使用@ Massmaker的答案.
我的解决方案(适用于 iOS 8)首先在我的 viewController 中设置 viewDidLoad
self.tableView.rowHeight = UITableViewAutomaticDimension;
// this line is needed to cell`s textview change cause resize the tableview`s cell
self.tableView.estimatedRowHeight = 50.0;
Run Code Online (Sandbox Code Playgroud)
然后,结合Swift 中的这篇文章解决方案 和一些肮脏的想法,我在单元格中设置了一个属性,称为
@property (nonatomic, strong) UITableView *hostTableView;
Run Code Online (Sandbox Code Playgroud)
并在细胞-s中 -(void) textViewDidChange:(UITextView *)textView
CGFloat currentTextViewHeight = _textContainer.bounds.size.height;
CGFloat toConstant = ceilf([_textContainer sizeThatFits:CGSizeMake(_textContainer.frame.size.width, FLT_MAX)].height);
if (toConstant > currentTextViewHeight)
{
[_hostTableView beginUpdates];
[_hostTableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)
然后在viewController中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
textCell.hostTableView = self.tableView;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3085 次 |
最近记录: |