gpt4 book ai didi

ios - 如何让 UITableViewCell 适合整个 TextView,而不管文本的大小?

转载 作者:搜寻专家 更新时间:2023-10-31 22:34:06 25 4
gpt4 key购买 nike


我试图在每个表格单元格中显示一个文本字段。问题是我似乎无法让 UITableViewCell 足够高以适应整个 TextView。 UITableViewCell 不需要是动态的,因为数据是在 View 加载时加载到 UITextView 中的。
这是我的代码:

//
// chatViewController.swift
// collaboration
//
// Created by nick on 11/21/15.
// Copyright © 2015 Supreme Leader. All rights reserved.
//

import UIKit

class chatViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var testLabel: UILabel!

var messages: NSMutableArray = NSMutableArray()

var authors: NSMutableArray = NSMutableArray()

func tableView(tableView: UITableView, numberOfRowsInSection Section: Int) -> Int {
return self.authors.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! chatTableViewCell
cell.authorLabel?.text = self.authors.objectAtIndex(indexPath.row) as? String
return cell
}

override func viewDidLoad() {
super.viewDidLoad()
let receivedUsername = NSUserDefaults.standardUserDefaults().stringForKey("usernameToMessageWith")
testLabel.text = "\(receivedUsername! as String)"
authors.addObject("")
authors.addObject("You:")
CGRect frame = messagesTextView.frame;
frame.size = messagesTextView.contentSize;
messagesTextView.frame = frame;
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

}

我完全不知道该怎么做。请不要因为我可悲的搞砸范围尝试而取笑我。

最佳答案

如果您使用的是文本字段,则必须以编程方式计算高度并在显示单元格时设置它。如果它不可编辑,您可以使用 UILabel 执行以下操作。

在你viewDidLoad设置 tableView.estimatedRowHeight = <expected height>tableView.rowHeight = UITableViewAutomaticDimension

然后在您的自定义单元格中,您需要为 UILabel 的顶部和底部朝向 contentView 的边缘设置布局约束并设置 UILabels lines0

文本字段的可能解决方法:

同时使用文本字段和 UILabel,并将 UILabel 文本颜色设置为透明色。这样您就可以使用 UILabel 来调整高度,但仍然具有文本字段的功能。

关于ios - 如何让 UITableViewCell 适合整个 TextView,而不管文本的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com