i have a UITableView which has multiple Prototype cells (with different identifiers ) and i have a separate class for my Cells ! this is how i've created my TableViewController :-
import UIKit
class PostTableViewController: UITableViewController, UITextFieldDelegate {
var postArray = [["Sean Paul","Got To Love You"],["California","21 January 2018"],["Martin Garrix"]]
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44
}
override func viewDidAppear(animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
print(postArray[section].count)
return postArray[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if indexPath.section == 0 && indexPath.row == 0{
cell = tableView.dequeueReusableCellWithIdentifier("titleCell", forIndexPath: indexPath) as!
MultiLineTextInputTableViewCell
}
if indexPath.section == 0 && indexPath.row == 1 {
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!
MultiLineTextInputTableViewCell
}
if indexPath.section == 1 && indexPath.row == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("locationCell", forIndexPath: indexPath) as!
MultiLineTextInputTableViewCell
}
if indexPath.section == 1 && indexPath.row == 1 {
cell = tableView.dequeueReusableCellWithIdentifier("timeCell", forIndexPath: indexPath) as!
MultiLineTextInputTableViewCell
}
if indexPath.section == 2 && indexPath.row == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("recipientCell", forIndexPath: indexPath) as!
MultiLineTextInputTableViewCell
}
return cell
}
@IBAction func btnActionPost(sender: AnyObject) {
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
print( tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text) // here i tried to get the text of first cell but text is not in the cell's label it is inside the TextView which is inside UItableViewCell
tableView.cellForRowAtIndexPath(indexPath)
}
}
and this is how i've created my TableViewCellController:-
import UIKit
class MultiLineTextInputTableViewCell: UITableViewCell {
// @IBOutlet weak var titleLabel: UILabel?
@IBOutlet var textView: UITextView?
@IBOutlet var titleTxtField: UITextField!
@IBOutlet var locationTxtField: UITextField!
@IBOutlet var timeTxtField: UITextField!
@IBOutlet var recipientTxtField: UITextField!
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 textView?.text ?? ""
}
set {
if let textView = textView {
textView.text = newValue
textViewDidChange(textView)
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
// Disable scrolling inside the text view so we enlarge to fitted size
textView?.scrollEnabled = false
textView?.delegate = self
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
textView?.becomeFirstResponder()
} else {
textView?.resignFirstResponder()
}
}
}
extension MultiLineTextInputTableViewCell: 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
}
}
}
i tried this for getting the text from the cell:- but its not the proper approach
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
print( tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text) // here i tried to get the text of first cell but text is not in the cell's label it is inside the TextView which is inside UItableViewCell
tableView.cellForRowAtIndexPath(indexPath)
if anybody knows then please guide me it will be very helpful for me :)