OGeek|极客世界-中国程序员成长平台

标题: iphone - 在 UITableView reloadData 之后在 UITextView 中设置选择? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:19
标题: iphone - 在 UITableView reloadData 之后在 UITextView 中设置选择?

我在 UITableViewCell 中放置了一个 UITextView。一旦用户点击某个按钮,我将调用 [UITableView relodaData] 来更新 UITextView 中的内容。

我的目标是在调用 reloadData 后在 UITextView 中设置选择。是否有任何回调方法可用于 [UITableView reloadData] 方法,就像 performBatchUpdates 可用于 UICollectionView.



Best Answer-推荐答案


重新加载表格 View 通常不会让第一响应者辞职。必须在您的代码中,例如在 cellForRowAtIndexPath 中。

无论如何,要恢复失去的焦点,跟踪哪个 textView 正在编辑(如果只有一个,那就简单多了),在调用 reloadData 之前记住它,然后恢复光标。

- (void)textViewDidBeginEditingUITextView *)textView {
    _currentEditingTextView = textView;
}
- (void)textViewDidEndEditingUITextView *)textView {
    _currentEditingTextView = nil;
}
- (void)yourUpdate {
    UITextView *editingTextView = _currentEditingTextView; 
    [tableView reloadData];
    [editingTextView performSelectorselector(becomeFirstResponder) 
                           withObject:nil 
                           afterDelay:0.1]
}

另外,如果您只需要更新一个单元格,请通过 reloadRowsAtIndexPaths 方法进行。或者,如果您只需要更新 textview 的文本,直接这样做:

((YourCellClass *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:yourRow inSection:0]]).textView.text = ...;

关于iphone - 在 UITableView reloadData 之后在 UITextView 中设置选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629145/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4