Swift tableview cell auto height with auto height label - swift

I have some problems with swift.
Need to make tableview and in every cell to have image with text inside.
This is what i made so far:
First problem:
Label should be auto height, now it breaks string..
Second problem:
Image needs to be auto height too, and to depends on label height.
Third problem:
Row needs to be autoheight depending on its inside.
TableViewController code:
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44.0;
tableView.tableFooterView = UIView(frame: CGRectZero)
tableView.registerNib(UINib(nibName: "QuoteTableViewCell", bundle: nil), forCellReuseIdentifier: "QuoteCell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return results.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("QuoteCell", forIndexPath: indexPath) as! QuoteTableViewCell
cell.item = results[indexPath.row]
return cell
}
Cell code:
class QuoteTableViewCell: UITableViewCell {
var item: Quote! {
didSet {
setupCell()
}
}
#IBOutlet weak var imageField: UIView!
#IBOutlet weak var textField: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func setupCell() {
self.imageField.backgroundColor = UIColor(patternImage: UIImage(named: "Bg")!)
textField.text = item.text
}
}

To have the label adapt its height to its contents, set its Lines number to 0 in Storyboard.
If you want to have an image next to it, why not to put both controls into one horizontal StackView?
And to have the tableView row height adapt to the cell contents (i.e. label height), just set it's rowHeight to automatic:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 90.0;

Use this metod to have that functionality
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let lineBrakingMode = NSLineBreakMode.ByCharWrapping
let paragragh:NSMutableParagraphStyle = NSMutableParagraphStyle()
paragragh.lineBreakMode = lineBrakingMode
let attributes:NSDictionary = [NSFontAttributeName: self.textField.font!, NSParagraphStyleAttributeName:paragragh]
let TextSize = self.textField.text!.sizeWithAttributes(attributes)
let TextHeight = TextSize.height + 5;
UIView.animateKeyframesWithDuration(0.2, delay: 2.0, options: UIViewKeyframeAnimationOptions.AllowUserInteraction, animations: { () -> Void in
self.imageField.frame.height = TextHeight
}, completion: nil)
return TextHeight
}
}

Related

Dynamically resize UITableViewCell at runtime on tvOS

I currently have a UIViewController with the following hierarchy:
UIViewController
-- UIView
---- UITableView
------ UITableViewCell
-------- UICollectionView
---------- UICollectionViewCell
-------- UIView
---------- UIStackView
------------ UILabel
------------ UILabel
So basically, I have a UITableViewCell, containing an UICollectionView and an UIView, with the following constraints defined in Interface Builder:
UICollectionView.leading = Superview.leading
UICollectionView.trailing = Superview.trailing
UICollectionView.top = Superview.top
UICollectionView.bottom = UIView.top
UIView.leading = Superview.leading
UIView.trailing = Superview.trailing
UIView.bottom = Superview.bottom
The UICollectionView is also set up with a horizontal flow layout.
In my ViewController, I have also overridden the heightForRowAtIndexPath datasource function for the UITableView.
The result is a vertically scrolling list of UITableViewCells, and each UITableViewCell will have a horizontally scrolling list of UIImageViews. The UIView and its two UILabels will display relevant information to the UIImageView when it is focused.
What I am trying to achieve is to hide the UIView containing the two UILabels when the focus moves to a different UITableViewCell. I am able to trap and detect the change in focus by overriding the didUpdateFocusInContext function. I am able to reduce the height of the previously-focused UITableViewCell in the UICollectionViewDelegate, however, the gap between the previously-focused and currently-focused UITableViewCells remain unchanged.
Attempts to set the UITableView.RowHeight to UITableViewAutomaticDimension, setting UITableView.EstimatedRowHeight to an arbitrary figure, removing the overridden heightForRowAtIndexPath function, causes the UICollectionViews to not load at all.
Does anyone have a good suggestion on what I could try?
Edit: Sample source code
ViewController
// ==== ViewController.swift ====
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
let cellIdentifier = "\(CVTableViewCell.self)"
override func viewDidLoad() {
super.viewDidLoad()
setupControls()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
setupControls()
}
func setupControls() {
setupTableView()
}
func setupTableView() {
tableView.dataSource = self
tableView.delegate = self
// causes collectionView in CVTableViewCell to not load
// tableView.rowHeight = UITableViewAutomaticDimension
// tableView.estimatedRowHeight = 200
tableView.register(UINib(nibName: cellIdentifier, bundle: nil),
forCellReuseIdentifier: cellIdentifier)
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 316.0 + 106.0 // 316.0 for image height, 106.0 for labelContainerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? CVTableViewCell else {
fatalError("Unable to dequeue cell")
}
return dequeuedCell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
return false
}
}
CVTableViewCell
// ==== CVTableViewCell.swift ====
import Foundation
import UIKit
class CVTableViewCell: UITableViewCell {
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var labelContainerView: UIView!
#IBOutlet weak var labelContainerStackView: UIStackView!
#IBOutlet weak var firstLabel: UILabel!
#IBOutlet weak var secondLabel: UILabel!
let cellIdentifier = "\(CVCollectionViewCell.self)"
override func awakeFromNib() {
super.awakeFromNib()
setupControls()
}
override func prepareForReuse() {
super.prepareForReuse()
setupControls()
}
func setupControls() {
setupTableViewCell()
setupCollectionView()
setupLabelContainerView()
setupLabelContainerStackView()
setupLabels()
}
func setupTableViewCell() {
contentView.backgroundColor = .clear
}
func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: 316.0, height: 316.0)
layout.minimumInteritemSpacing = 0.0
layout.minimumLineSpacing = 50.0
collectionView.dataSource = self
collectionView.delegate = self
collectionView.backgroundColor = .clear
collectionView.clipsToBounds = false
collectionView.collectionViewLayout = layout
collectionView.register(UINib(nibName: cellIdentifier, bundle: nil),
forCellWithReuseIdentifier: cellIdentifier)
}
func setupLabelContainerView() {
labelContainerView.backgroundColor = .clear
}
func setupLabelContainerStackView() {
labelContainerStackView.backgroundColor = .clear
labelContainerStackView.distribution = .fillEqually
}
func setupLabels() {
firstLabel.text = "Look at me"
secondLabel.text = "I refuse to go away"
}
}
extension CVTableViewCell: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let dequeuedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? CVCollectionViewCell else {
fatalError("Unable to dequeue cell")
}
dequeuedCell.imageView.adjustsImageWhenAncestorFocused = true
dequeuedCell.imageView.image = #imageLiteral(resourceName: "stackoverflow")
return dequeuedCell
}
}
extension CVTableViewCell: UICollectionViewDelegate {
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocus(in: context, with: coordinator)
// if next focused view is within current collectionview, show labelContainerView
// else hide
if let cell = context.nextFocusedView as? CVCollectionViewCell,
let _ = collectionView.indexPath(for: cell) {
labelContainerView.isHidden = false
} else {
labelContainerView.isHidden = true
}
}
}
CVCollectionViewCell
// ==== CVCollectionViewCell.swift ====
import Foundation
import UIKit
class CVCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
setupControls()
}
override func prepareForReuse() {
super.prepareForReuse()
setupControls()
}
func setupControls() {
setupImageView()
}
func setupCollectionViewCell() {
backgroundColor = .clear
}
func setupImageView() {
imageView.image = nil
}
}

Dynamic UITableView with images

There are similar questions, but non of the answers worked for me. In a dynamic table I want to display images that have different heigh. Each cell has a UIImageView with contentMode = .scaleAspectFit so the image nicely takes the width of the table and takes the height as much as needed.
Cell has 4 constraints:
Table view controller:
class TableTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ImageTableViewCell.self), for: indexPath) as! ImageTableViewCell
let image = indexPath.row == 0 ? UIImage(named: "1.jpg")! : UIImage(named: "2.jpg")!
cell.customImageView.image = image
return cell
}
}
Result:
As you can see that the height of the cell is incorrect (red background of the image view on top and bottom of the image view). I believe this happens because intrinsicContentSize of the image view is equal to the image size and thats why the height of the cell is calculated incorrectly (content mode is not taken into account). I tried calculating height of the image and adding height constraint for the image view:
cell.heightConstraint.constant = cell.frame.width * image.size.height / image.size.width
but it breaks cell's content view constraints.
The project can be downloaded here>>
In your ImageTableViewCell.swift
import UIKit
class ImageTableViewCell: UITableViewCell {
#IBOutlet weak var customImageView: UIImageView!
internal var aspectConstraint : NSLayoutConstraint? {
didSet {
if oldValue != nil {
customImageView.removeConstraint(oldValue!)
}
if aspectConstraint != nil {
customImageView.addConstraint(aspectConstraint!)
}
}
}
override func prepareForReuse() {
super.prepareForReuse()
aspectConstraint = nil
}
func setPostedImage(image : UIImage) {
let aspect = image.size.width / image.size.height
aspectConstraint = NSLayoutConstraint(item: customImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: customImageView, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
customImageView.image = image
}
}
And into your TableTableViewController.swift your cellForRowAt method will be:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell
let image = imageArr[indexPath.row]
cell.setPostedImage(image: image!)
return cell
}
And declare your imageArr this way:
let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
And your compete code will be:
import UIKit
class TableTableViewController: UITableViewController {
let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell
let image = imageArr[indexPath.row]
cell.setPostedImage(image: image!)
return cell
}
}
And THIS will be your result.
EDIT:
To fix constraint issue set aspectConstraint priority to 999 and aspectConstraint will be:
internal var aspectConstraint : NSLayoutConstraint? {
didSet {
if oldValue != nil {
customImageView.removeConstraint(oldValue!)
}
if aspectConstraint != nil {
aspectConstraint?.priority = 999 //add this
customImageView.addConstraint(aspectConstraint!)
}
}
}

Selecting row in tableview, removes custom cell

I have a Viewcontroller with a Searchbar at the top with a tableview below. The tableview has a custom cell with 2 labels in it. My problem is that when i run the app and i select a row/cell everything inside the cell disappears. I then force the blank cell outside the visible area of the tableview, so it will be re-used. That's when everything inside the cell is back. Does anyone know why it behaves like this?
My Custom cell class (ContactCell.swift):
import UIKit
class ContactCell: UITableViewCell {
#IBOutlet var lblContactName: UILabel!
#IBOutlet var lblContactTitle: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
My ViewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
My Delegate and Datasource:
extension contactsTabelViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("contactCell", forIndexPath: indexPath) as! ContactCell
if let label = cell.lblContactName{
label.text = "This is a name"
}
if let label3 = cell.lblContactTitle{
label3.text = "This is a title"
}
return ContactCell()
}
}
The problem that caused this problem was that i returned ContactCell() instead of the variable cell
Solution was:
Change this:
return ContactCell()
to this:
return cell
in the cellForRowAtIndexPath function.

TableView Determine Third Cell From Top

I constantly am trying to determine the third cell from the top of my table view. Basically, what that means is, the third cell from the top will always look different from all the others (i.e. the text color will change etc.).
I figured since the cells are reused, I would always be able to access the third cell like this:
    
if (indexPath.row == 2) {
    
    }
Unfortunately, it doesn't seem to be working like that. When I go ahead and print the indexPath.row the numbers continue to increase all the way from 0 to 12... Now this is understandable since it is that cells row, but how may I go about accessing the third row always from the top. Here is the original approach I took:
    
override func scrollViewDidScroll(scrollView: UIScrollView) {
    
        let indexPath: NSIndexPath = self.tableView.indexPathsForVisibleRows![0]
            
        if (indexPath.row == 2) {
          // Print or whatever
    
            }
        }
    }
So how can I go about accessing the third row always from the top of the tableView?
Thank you!
Here is an example project. I tried running it in the simulator and it seems to work fine.
Here is a screenshot of XCode so you can see Main.Storyboard.
Also here is a copy of the code in ViewController.swift:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var indexOfThirdVisible: Int!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
self.navigationController?.navigationBar.barStyle = .BlackTranslucent
self.navigationController?.navigationBar.tintColor = UIColor.orangeColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let indexPath = self.tableView.indexPathsForVisibleRows![0]
indexOfThirdVisible = indexPath.row + 2
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexOfThirdVisible, inSection: indexPath.section)) as! TableViewCell
cell.label.textColor = UIColor.orangeColor()
let cellAbove = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexOfThirdVisible - 1, inSection: indexPath.section)) as! TableViewCell
let cellBelow = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexOfThirdVisible + 1, inSection: indexPath.section)) as! TableViewCell
cellAbove.label.textColor = UIColor.whiteColor()
cellBelow.label.textColor = UIColor.whiteColor()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
// heightForRowAtIndexPath
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
// configure cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
return cell
}
}
Here is TableViewCell.swift:
import UIKit
class TableViewCell: UITableViewCell {
#IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Sorry if the indentation is messed up.
Have a good day and let me know if you need any more help!
I just implemented only logic, you can customise it little bit according to you.
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
let indexPath : NSIndexPath = (tv.indexPathsForVisibleRows as! NSArray).objectAtIndex(0) as! NSIndexPath
let thirdRow = indexPath.row + 2
if thirdRow <= dataArr.count{// dataArr is your no of rows
let thirdIndexPath = NSIndexPath(forRow: thirdRow, inSection: 0)
let cell = tv.cellForRowAtIndexPath(thirdIndexPath)
// ---- here you can perform any task with third cell----
}
}
And please let me know if anything is wrong

how can i get the text Value of a textView which is inside a UITableViewCell

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 :)
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.
In Swift 4 -
let indexPath = IndexPath(row: 1, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! MyCustomCell
print(cell.textView.text)