2

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 :)

atalayasa
  • 3,310
  • 25
  • 42
  • I recently described in detail the communication between a custom `UITableViewCell`, a `UITableViewController` and a `UIViewController`, possibly it can help you to cover that and apply to your case. See http://stackoverflow.com/questions/34247239/global-variable-and-optional-binding-in-swift/34275632#34275632 – dfrib Jan 04 '16 at 22:30
  • @dfri thanks for helping :) –  Jan 04 '16 at 23:00

2 Answers2

7

cellForRowAtIndexPath(_:) returns a UITableViewCell? which has a textLabel property, which itself is a UILabel — but you never assign anything to the textLabel on the UITableViewCell; you assign to your custom textView property.

Try this instead:

let indexPath = NSIndexPath(forRow: 1, inSection: 0)
let multilineCell = tableView.cellForRowAtIndexPath(indexPath) as? MultiLineTextInputTableViewCell // we cast here so that you can access your custom property.
print(multilineCell?.textView.text)

This way you are operating on your custom cell's TextView.

Alex Popov
  • 2,509
  • 1
  • 19
  • 30
0

In Swift 4 -

let indexPath = IndexPath(row: 1, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! MyCustomCell
print(cell.textView.text)
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31