Skip to content

Instantly share code, notes, and snippets.

@nemotoo
Created July 29, 2015 11:56
Show Gist options
  • Save nemotoo/86b04a8af06371a51a3f to your computer and use it in GitHub Desktop.
Save nemotoo/86b04a8af06371a51a3f to your computer and use it in GitHub Desktop.
SelfSizing TableViewCell with Swift 2.0 Beta Raw
// References
// https://github.com/damienpontifex/BlogCodeSamples/tree/master/SelfSizingTextViewTableCell
// https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/
//
// In UITableViewController
// tableView.rowHeight = UITableViewAutomaticDimension;
// tableView.estimatedRowHeight = DefaultCellHeight;
//
import UIKit
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
}
}
}
class SelfSizingTextViewTableCell: UITableViewCell {
private let textView = UITextView()
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
textView.scrollEnabled = false
textView.delegate = self
contentView.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false;
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.blackColor().CGColor
//Width
textView.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[textView]|", options: .AlignAllCenterX, metrics: nil, views: ["textView":textView]))
//Height
textView.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[textView(>=\(DefaultCellHeight))]|", options: .AlignAllTop, metrics: nil, views: ["textView":textView]))
}
/// Custom setter so we can initialise the height of the text view
var textString: String {
get {
return textView.text ?? ""
}
set {
textView.text = newValue
textViewDidChange(textView)
}
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
textView.becomeFirstResponder()
} else {
textView.resignFirstResponder()
}
}
}
extension SelfSizingTextViewTableCell: 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)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment