1
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cellFrame = CGRectMake(0, 0, tableView.frame.width, 100)
    var retCell = UITableViewCell(frame: cellFrame)
     var textView = UITextView(frame: CGRectMake(8,30,tableView.frame.width-8-8,65))
                textView.layer.borderColor = UIColor.blueColor().CGColor
                textView.layer.borderWidth = 1
                                    retCell.addSubview(textView)
                                    if(reqGrpIndex == 0){
                    reqGrpIndex = reqGrpIndex + 1
                }
}

func textViewDidChange(textView: UITextView){
    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame;
}

I saw some post about resizing the textview. However i failed to do that after applying those code. please help. any help will be appreciated.

hungry
  • 13
  • 3
  • have you set textview `delegate` ? – xmhafiz Aug 29 '16 at 07:24
  • no. i didn't. how to set its delegation. textView.delegate = self?? – hungry Aug 29 '16 at 07:26
  • yes. so it will reflect the delegate methods such as `textViewDidChange `. Refer to this http://stackoverflow.com/questions/25064465/uitextview-data-change-swift?answertab=votes#tab-top – xmhafiz Aug 29 '16 at 07:28
  • but it prompted an error "cannot assign value of type "ViewController" to type "UITextViewDelegate?" – hungry Aug 29 '16 at 07:30
  • have you added `UITextViewDelegate` ? as `class YourViewController: UIViewController, UITextViewDelegate` – xmhafiz Aug 29 '16 at 07:34
  • it works, thank you so much – hungry Aug 29 '16 at 07:37
  • great, just added the answer – xmhafiz Aug 29 '16 at 07:37
  • Please take note that, avoid adding subviews to cell as it is reusable. Instead, use storyboard which will create once or use `viewWithTag` – xmhafiz Aug 29 '16 at 07:40
  • then how can i add views programmatically instead of adding subviews to cell – hungry Aug 29 '16 at 07:44
  • the issue will occur when scrolling the tableview, cell will keep adding more subviews if using `addSubview`. the solution is here, can refer this thread but it is in objective c http://stackoverflow.com/questions/18379280/duplicate-data-when-scrolling-a-uitableview-with-custom-subviews?answertab=votes#tab-top – xmhafiz Aug 29 '16 at 07:55

1 Answers1

2

Set delegate to uitextview

//If your class is not conforms to the UITextViewDelegate protocol you will not be able to set it as delegate to UITextView
class YourViewController: UIViewController, UITextViewDelegate { 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        textView.delegate = self
    }

    func textViewDidChange(textView: UITextView) { 
        // your resize things
    }
}
xmhafiz
  • 3,482
  • 1
  • 18
  • 26