Swift:如何以编程方式添加textView并将其调整为单元格?

时间:2014-12-26 19:31:20

标签: ios uitableview swift textview

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

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

cell.addSubview(textView)

我发现了一个类似于客观C的问题,但不仅我不了解客观C,还没有完全涵盖这个问题。

2 个答案:

答案 0 :(得分:2)

所以你必须做以下事情,我认为它评论得很好。请记住:您必须在故事板中为表视图设置至少1个动态原型单元格,然后自定义您希望的方式。然后使用自定义UITableViewCell类中的插座属性连接所有插座 - >请参阅以下内容:

// 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
}
}

顺便说一句,将文字视图的大小调整为内部文字只需:cell.myTextView.sizeToFit()

答案 1 :(得分:2)

玩了几个小时并从奥利弗学到一堆东西后,我终于明白了。事实证明,将textView放入单元格非常简单,但是在键入时单元格和textView动态增长显然是另一项壮举。这主要是为了实现目标,但我不满意手动将高度设置为100。

    var cellIdentifier = ""

    if indexPath.section == 1 {
        cellIdentifier = "CellSection1"
        tableView.rowHeight = 100
        cell.frame.size.height = 100
        let cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
        var textView = UITextView(frame: CGRect(x: 0, y: 0, width: cell.frame.size.width, height: cell.frame.size.height))
        cell.addSubview(textView)
        return cell

    } else {

        cellIdentifier = "CellSection0"
        let cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
        cell.textLabel?.font = UIFont.systemFontOfSize(12)
        cell.textLabel?.text = entry.thoughtText
        cell.textLabel?.numberOfLines = 0
        return cell
    }