我在自定义 TableView
单元格中有一个 UITextView
。我希望 TextView 默认滚动到顶部。我通常在常规 View 中这样做:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let fixedWidth = bioView.frame.size.width
bioView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
let newSize = bioView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
var newFrame = bioView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
bioView.frame = newFrame;
bioView.scrollRangeToVisible(NSRange(location:0, length:0))
}
bioView 是我的 TextView 对象。但是,这在我的情况下不起作用,因为我无法在 ViewController
类中引用 textview
并且只能在 CustomCell
类并且该类不能覆盖 viewDidLayoutSubviews
。有帮助吗?
最佳答案
试试这个。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
[cell.textView scrollRangeToVisible:NSMakeRange(0, 0)];
return cell;
}
swift 3.0:
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! DemoCell
cell.textView.scrollRangeToVisible(NSRange(location:0, length:0))
有效!但是,必须滚动表格才能使更改生效,这是我目前找到的最佳方式。谢谢大家!
关于iOS - 当 TextView 位于表格 View 单元格中时将其滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42105768/