gpt4 book ai didi

ios - Swift:如何以编程方式将 textView 添加到单元格并调整其大小?

转载 作者:可可西里 更新时间:2023-11-01 00:53:31 30 4
gpt4 key购买 nike

我正在尝试以编程方式将 textView 添加到单元格,因为我无法在这种情况下使用 Storyboard。下面的代码是我对如何实现这一点的不完全理解。我不知道如何调整它的大小以不仅匹配单元格的大小(宽度和高度),而且调整单元格和 textView 的大小以匹配输入的文本量。

var textView: UITextView = UITextView(//what goes here??)

cell.addSubview(textView)

我发现了一个关于 objective-c 的类似问题,但我不仅不知道 objective-c ,而且还没有完全涵盖问题。

最佳答案

因此您必须执行以下操作,我认为它已得到很好的评论。请记住:您必须在 Storyboard中为您的 TableView 设置至少 1 个动态原型(prototype)单元格,然后自定义您想要的方式。然后将所有 socket 与自定义 UITableViewCell 类中的 socket 属性连接起来 -> 请参阅以下内容:

// you don't need to do anything else here when creating your table view cell in your storyboard
// as all the required functions can be used from the superclass UITableViewCell
class MyTableViewCell: UITableViewCell {
@IBOutlet var myTextView: UITextView!
}

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//already initialized from the storyboard
@IBOutlet var myTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

//need to set table view's delegate and data source
myTableView.delegate = self
myTableView.dataSource = self
}

// MARK: TableView source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5 //usually you have an array and you return array.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//watch out, your identifier has to be the same as in the storyboard.
// When selecting your cell in the storyboard you can set this identifier in the attributes inspector
let cell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath) as MyTableViewCell
cell.myTextView.text = "myName" //usually you do: array[indexPath.row] -> example for an array of strings
return cell
}

//when setting and text view, you probably want a quite big table view cell so you have to do the following
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60 //the height of your table view cell, the default value is 44
}
}

希望这能奏效,如果您有任何问题,请告诉我

更新:以编程方式

class MyTableViewCell: UITableViewCell {
var myTextView: UITextView!
override init() {
super.init()
myTextView = UITextView(frame: self.frame)
self.addSubview(myTextView)
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var myTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

//need to set table view's delegate and data source
//maybe style grouped looks better for more section table view
myTableView = UITableView(frame: UIScreen.mainScreen().applicationFrame, style: .Grouped)
myTableView.backgroundColor = UIColor.whiteColor()
myTableView.delegate = self
myTableView.dataSource = self
}

// MARK: TableView source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = UITableViewCell()
cell.textLabel!.text = "myText"
return cell
} else { //custom cell for section 2
let cell = MyTableViewCell()
cell.myTextView.text = "myName"
return cell
}
}

//when setting and text view, you probably want a quite big table view cell so you have to do the following
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
return 44
}
return 60 //the height of your table view cell, the default value is 44
}
}

顺便说一下,要根据里面的文本调整 TextView 的大小,只需执行以下操作:cell.myTextView.sizeToFit()

关于ios - Swift:如何以编程方式将 textView 添加到单元格并调整其大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27661133/

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