I have a custom UITableViewCell
and I\'m trying to resize the UITextView
inside it based on the content size. I\'m on iOS7 and using Autolayout.>
I had the same issue but in a different situation. I have UItableview with two custom cells.
First Custom cell - self expanding textview. (like email type message box)
Second Custom Cell - Static image.
Have a look at my code. You will get an insight to automatic resizing of cells.
// ViewController.swift
// ListWrap
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate, UITextViewDelegate {
@IBOutlet var listWrapTableView: UITableView!
//CustomCells
var CellIdentifier: String = "ListWrapTableViewCellID"
var tapGesture: UITapGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
//Adding Tap Gesture To Table
tapGesture = UITapGestureRecognizer(target: self, action: "tapRecognized:")
self.listWrapTableView.addGestureRecognizer(tapGesture)
tapGesture.cancelsTouchesInView = false
tapGesture.enabled = true
}
func tapRecognized(recognizer: UITapGestureRecognizer){
self.listWrapTableView.endEditing(true)
}
func textViewDidBeginEditing(textView: UITextView) {
if (CellIdentifier == "ListWrapTableViewCellID")
{
tapGesture.enabled = true
}
else
{
tapGesture.enabled = false
}
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
self.listWrapTableView.rowHeight = UITableViewAutomaticDimension
return self.listWrapTableView.rowHeight
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (indexPath.row == 0)
{
let surveyCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! ListWrapTableViewCell
return surveyCell
}
else if (indexPath.row == 1)
{
let reportsCell = tableView.dequeueReusableCellWithIdentifier("ListWrapSecondTableViewCellID")! as! ListWrapSecondTableViewCell
return reportsCell
}
else
{
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "")
return cell
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
}
}
The first Custom cell:
// ListWrapTableViewCell.swift
// ListWrap
import UIKit
class ListWrapTableViewCell: UITableViewCell{
@IBOutlet var listWrapTextView: UITextView!
//
// override init?(style: UITableViewCellStyle, reuseIdentifier: String!) {
// super.init(style: style, reuseIdentifier: reuseIdentifier)
// }
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
/// Custom setter so we can initialise the height of the text view
var textString: String {
get {
return listWrapTextView.text
}
set {
listWrapTextView.text = newValue
textViewDidChange(listWrapTextView)
}
}
override func awakeFromNib() {
super.awakeFromNib()
listWrapTextView.scrollEnabled = false
listWrapTextView.delegate = self
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
listWrapTextView.becomeFirstResponder()
} else {
listWrapTextView.resignFirstResponder()
}
}
}
extension ListWrapTableViewCell: UITextViewDelegate {
func textViewDidChange(textView: UITextView) {
let size = textView.bounds.size
let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.max))
// Resize the cell only when cell's size is changed
if size.height != newSize.height {
UIView.setAnimationsEnabled(false)
tableView?.beginUpdates()
tableView?.endUpdates()
UIView.setAnimationsEnabled(true)
if let thisIndexPath = tableView?.indexPathForCell(self) {
tableView?.scrollToRowAtIndexPath(thisIndexPath, atScrollPosition: .Bottom, animated: false)
}
}
}
}
extension UITableViewCell {
/// Search up the view hierarchy of the table view cell to find the containing table view
var tableView: UITableView? {
get {
var table: UIView? = superview
while !(table is UITableView) && table != nil {
table = table?.superview
}
return table as? UITableView
}
}
}
The second custom cell:
// ListWrapSecondTableViewCell.swift
// ListWrap
import UIKit
class ListWrapSecondTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
attaching storyBoard for further reference.