iOS - 当 TextView 位于表格 View 单元格中时将其滚动到顶部

标签 ios swift uitableview uitextview

我在自定义 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/

相关文章:

ios - UIImage 不在 UIImageView 的所需位置

ios - If else语句取决于文本字段(swif3)

swift - 由于 Style.spinning 已弃用,如何在 macOS Catalina 上正确添加微调器?

ios - 通过 NSTimer 在 ViewDidLoad 中调用函数 - swift

ios - 如何将一个 IBOutlet 连接到容器 View Controller 中的 TableView Controller

ios - UITableViewCells 重复内容的问题

ios - Objective C TableView 清晰 Json 解析

ios - Swift 中的 appearanceWhenContainedIn

ios - 是否可以在不删除整个项目的情况下删除 iOS 钥匙串(keychain)中帐户项目的密码?

swift - ios中如何让一个单元格容纳多个文本标签