How to get Label text of UITableView Cell using Swift?












2















I want to make simple UITabelView. I registered it's cell with my custom class which named as backgroundviewcell. I want to get Label of the selected cell. I tried many times but the output is coming nil value. I have also tried solution from stack overflow but it does not work for me. This is my cellForRowAt indexPath code :



 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell


let imageNames = sections[indexPath.section].images[indexPath.row]

cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
//cell.detailTextLabel?.textColor = UIColor.red
//cell.isHighlighted = false

cell.backgroundColor = UIColor.clear
cell.iconview.image = UIImage(named: imageNames)

return cell
}


This is my didSelectRowAt indexPath code :



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {


let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)!

celltext = currentCell.textLabel!.text
performSegue(withIdentifier: "showPlayer", sender: self)
}


and my Segue Method is :



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPlayer" {

let playerVC = segue.destination as! PlayerViewController
playerVC.trackName = (celltext)! as String


}
}









share|improve this question




















  • 1





    You shouldn't. You should get it from your tableview data source

    – Leo Dabus
    Jan 27 '17 at 5:56











  • Use indexPathForSelectedRow to access your array element

    – Leo Dabus
    Jan 27 '17 at 5:57






  • 1





    please all var in lowerCamelCase - because all classes always in UpperCamelCase

    – muescha
    Jan 27 '17 at 6:01











  • Please show us your code in cellForRowAtIndexPath methods and also tell us why you want to get Label from Cell.

    – Usman Javed
    Jan 27 '17 at 6:02











  • Thanks for suggestions @muesha.

    – Chetan Lodhi
    Jan 27 '17 at 6:13
















2















I want to make simple UITabelView. I registered it's cell with my custom class which named as backgroundviewcell. I want to get Label of the selected cell. I tried many times but the output is coming nil value. I have also tried solution from stack overflow but it does not work for me. This is my cellForRowAt indexPath code :



 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell


let imageNames = sections[indexPath.section].images[indexPath.row]

cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
//cell.detailTextLabel?.textColor = UIColor.red
//cell.isHighlighted = false

cell.backgroundColor = UIColor.clear
cell.iconview.image = UIImage(named: imageNames)

return cell
}


This is my didSelectRowAt indexPath code :



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {


let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)!

celltext = currentCell.textLabel!.text
performSegue(withIdentifier: "showPlayer", sender: self)
}


and my Segue Method is :



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPlayer" {

let playerVC = segue.destination as! PlayerViewController
playerVC.trackName = (celltext)! as String


}
}









share|improve this question




















  • 1





    You shouldn't. You should get it from your tableview data source

    – Leo Dabus
    Jan 27 '17 at 5:56











  • Use indexPathForSelectedRow to access your array element

    – Leo Dabus
    Jan 27 '17 at 5:57






  • 1





    please all var in lowerCamelCase - because all classes always in UpperCamelCase

    – muescha
    Jan 27 '17 at 6:01











  • Please show us your code in cellForRowAtIndexPath methods and also tell us why you want to get Label from Cell.

    – Usman Javed
    Jan 27 '17 at 6:02











  • Thanks for suggestions @muesha.

    – Chetan Lodhi
    Jan 27 '17 at 6:13














2












2








2








I want to make simple UITabelView. I registered it's cell with my custom class which named as backgroundviewcell. I want to get Label of the selected cell. I tried many times but the output is coming nil value. I have also tried solution from stack overflow but it does not work for me. This is my cellForRowAt indexPath code :



 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell


let imageNames = sections[indexPath.section].images[indexPath.row]

cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
//cell.detailTextLabel?.textColor = UIColor.red
//cell.isHighlighted = false

cell.backgroundColor = UIColor.clear
cell.iconview.image = UIImage(named: imageNames)

return cell
}


This is my didSelectRowAt indexPath code :



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {


let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)!

celltext = currentCell.textLabel!.text
performSegue(withIdentifier: "showPlayer", sender: self)
}


and my Segue Method is :



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPlayer" {

let playerVC = segue.destination as! PlayerViewController
playerVC.trackName = (celltext)! as String


}
}









share|improve this question
















I want to make simple UITabelView. I registered it's cell with my custom class which named as backgroundviewcell. I want to get Label of the selected cell. I tried many times but the output is coming nil value. I have also tried solution from stack overflow but it does not work for me. This is my cellForRowAt indexPath code :



 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell


let imageNames = sections[indexPath.section].images[indexPath.row]

cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
//cell.detailTextLabel?.textColor = UIColor.red
//cell.isHighlighted = false

cell.backgroundColor = UIColor.clear
cell.iconview.image = UIImage(named: imageNames)

return cell
}


This is my didSelectRowAt indexPath code :



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {


let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)!

celltext = currentCell.textLabel!.text
performSegue(withIdentifier: "showPlayer", sender: self)
}


and my Segue Method is :



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPlayer" {

let playerVC = segue.destination as! PlayerViewController
playerVC.trackName = (celltext)! as String


}
}






arrays swift uitableview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 27 '17 at 7:21









Nirav D

56.3k9103139




56.3k9103139










asked Jan 27 '17 at 5:51









Chetan LodhiChetan Lodhi

71114




71114








  • 1





    You shouldn't. You should get it from your tableview data source

    – Leo Dabus
    Jan 27 '17 at 5:56











  • Use indexPathForSelectedRow to access your array element

    – Leo Dabus
    Jan 27 '17 at 5:57






  • 1





    please all var in lowerCamelCase - because all classes always in UpperCamelCase

    – muescha
    Jan 27 '17 at 6:01











  • Please show us your code in cellForRowAtIndexPath methods and also tell us why you want to get Label from Cell.

    – Usman Javed
    Jan 27 '17 at 6:02











  • Thanks for suggestions @muesha.

    – Chetan Lodhi
    Jan 27 '17 at 6:13














  • 1





    You shouldn't. You should get it from your tableview data source

    – Leo Dabus
    Jan 27 '17 at 5:56











  • Use indexPathForSelectedRow to access your array element

    – Leo Dabus
    Jan 27 '17 at 5:57






  • 1





    please all var in lowerCamelCase - because all classes always in UpperCamelCase

    – muescha
    Jan 27 '17 at 6:01











  • Please show us your code in cellForRowAtIndexPath methods and also tell us why you want to get Label from Cell.

    – Usman Javed
    Jan 27 '17 at 6:02











  • Thanks for suggestions @muesha.

    – Chetan Lodhi
    Jan 27 '17 at 6:13








1




1





You shouldn't. You should get it from your tableview data source

– Leo Dabus
Jan 27 '17 at 5:56





You shouldn't. You should get it from your tableview data source

– Leo Dabus
Jan 27 '17 at 5:56













Use indexPathForSelectedRow to access your array element

– Leo Dabus
Jan 27 '17 at 5:57





Use indexPathForSelectedRow to access your array element

– Leo Dabus
Jan 27 '17 at 5:57




1




1





please all var in lowerCamelCase - because all classes always in UpperCamelCase

– muescha
Jan 27 '17 at 6:01





please all var in lowerCamelCase - because all classes always in UpperCamelCase

– muescha
Jan 27 '17 at 6:01













Please show us your code in cellForRowAtIndexPath methods and also tell us why you want to get Label from Cell.

– Usman Javed
Jan 27 '17 at 6:02





Please show us your code in cellForRowAtIndexPath methods and also tell us why you want to get Label from Cell.

– Usman Javed
Jan 27 '17 at 6:02













Thanks for suggestions @muesha.

– Chetan Lodhi
Jan 27 '17 at 6:13





Thanks for suggestions @muesha.

– Chetan Lodhi
Jan 27 '17 at 6:13












4 Answers
4






active

oldest

votes


















1














Instead of accessing the text from the cell's label you need to access the array that you have used to fill the tableView's cell data.



So you need to use UITableViewDelegate method didSelectRowAtindexPath and access the array that you are using with your tableView methods.



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
//Access the array that you have used to fill the tableViewCell
print(yourArray[indexPath.row])
}


Note: Once confirm that TableView delegate is properly connected with your ViewController so that it will call didSelectRowAt method when you select cell.



Edit: If you want to pass the data then try like this way.



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showPlayer", sender: indexPath) //Pass indexPath as sender instead of self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPlayer" {

let playerVC = segue.destination as! PlayerViewController
let indexPath = sender as! IndexPath
playerVC.trackName = sections[indexPath.section].items[indexPath.row]
}
}





share|improve this answer


























  • Actually i have 5 sections and in all section there is diff. type of array elements(string). Thats why i'm using text label of the cell for the next view controller to play my sound file.

    – Chetan Lodhi
    Jan 27 '17 at 6:12











  • @Chetan Check the edited answer once to pass the data

    – Nirav D
    Jan 27 '17 at 6:25











  • Thanks a lot. This code is working. You saved my life. :) @ Nirav D

    – Chetan Lodhi
    Jan 27 '17 at 6:32













  • @Chetan welcome mate :)

    – Nirav D
    Jan 27 '17 at 6:33











  • Accepted you answer. :)

    – Chetan Lodhi
    Jan 27 '17 at 6:47



















3














This is working for me:



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let cell = tableView.cellForRow(at: indexPath) as! YourCellType
let labelContent = cell.labelToAccess.text
}


First line creates an instance of your cell type (or standard cell, not my case).



The second saves the content of your label (labelToAccess in this case) in a constant.






share|improve this answer
























  • Thanks for reply.

    – Chetan Lodhi
    Oct 3 '17 at 6:05



















1














Try this 'Swift 4' -



func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let currentCellTxt = yourTableView.cellForRow(at: indexPath)! as? YourCustomTableViewCell

print(currentCellTxt?.lblYourName?.text) // 'lblYourName' that you defined in your 'YourCustomTableViewCell'

}





share|improve this answer





















  • 3





    Um...you already have indexPath in the function parameter of didSelectRowAtIndexPath...why would you even bother getting indexPath again? I am border line wanting to down vote.

    – Zhang
    Jan 27 '17 at 5:59











  • This is not working. @iDeveloper. Thanks for reply.

    – Chetan Lodhi
    Jan 27 '17 at 6:12



















0














You didn't set any text for textLabel. Hence it returns nil value.



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell



let imageNames = sections[indexPath.section].images[indexPath.row]

cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
//cell.detailTextLabel?.textColor = UIColor.red
//cell.isHighlighted = false

cell.backgroundColor = UIColor.clear
cell.iconview.image = UIImage(named: imageNames)

return cell


}



In didSelectRow func try to get value from Labeltittle.text not from textLabel.text



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)!

celltext = currentCell.Labeltittle.text
performSegue(withIdentifier: "showPlayer", sender: self)


}






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41887698%2fhow-to-get-label-text-of-uitableview-cell-using-swift%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Instead of accessing the text from the cell's label you need to access the array that you have used to fill the tableView's cell data.



    So you need to use UITableViewDelegate method didSelectRowAtindexPath and access the array that you are using with your tableView methods.



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
    //Access the array that you have used to fill the tableViewCell
    print(yourArray[indexPath.row])
    }


    Note: Once confirm that TableView delegate is properly connected with your ViewController so that it will call didSelectRowAt method when you select cell.



    Edit: If you want to pass the data then try like this way.



    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showPlayer", sender: indexPath) //Pass indexPath as sender instead of self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPlayer" {

    let playerVC = segue.destination as! PlayerViewController
    let indexPath = sender as! IndexPath
    playerVC.trackName = sections[indexPath.section].items[indexPath.row]
    }
    }





    share|improve this answer


























    • Actually i have 5 sections and in all section there is diff. type of array elements(string). Thats why i'm using text label of the cell for the next view controller to play my sound file.

      – Chetan Lodhi
      Jan 27 '17 at 6:12











    • @Chetan Check the edited answer once to pass the data

      – Nirav D
      Jan 27 '17 at 6:25











    • Thanks a lot. This code is working. You saved my life. :) @ Nirav D

      – Chetan Lodhi
      Jan 27 '17 at 6:32













    • @Chetan welcome mate :)

      – Nirav D
      Jan 27 '17 at 6:33











    • Accepted you answer. :)

      – Chetan Lodhi
      Jan 27 '17 at 6:47
















    1














    Instead of accessing the text from the cell's label you need to access the array that you have used to fill the tableView's cell data.



    So you need to use UITableViewDelegate method didSelectRowAtindexPath and access the array that you are using with your tableView methods.



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
    //Access the array that you have used to fill the tableViewCell
    print(yourArray[indexPath.row])
    }


    Note: Once confirm that TableView delegate is properly connected with your ViewController so that it will call didSelectRowAt method when you select cell.



    Edit: If you want to pass the data then try like this way.



    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showPlayer", sender: indexPath) //Pass indexPath as sender instead of self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPlayer" {

    let playerVC = segue.destination as! PlayerViewController
    let indexPath = sender as! IndexPath
    playerVC.trackName = sections[indexPath.section].items[indexPath.row]
    }
    }





    share|improve this answer


























    • Actually i have 5 sections and in all section there is diff. type of array elements(string). Thats why i'm using text label of the cell for the next view controller to play my sound file.

      – Chetan Lodhi
      Jan 27 '17 at 6:12











    • @Chetan Check the edited answer once to pass the data

      – Nirav D
      Jan 27 '17 at 6:25











    • Thanks a lot. This code is working. You saved my life. :) @ Nirav D

      – Chetan Lodhi
      Jan 27 '17 at 6:32













    • @Chetan welcome mate :)

      – Nirav D
      Jan 27 '17 at 6:33











    • Accepted you answer. :)

      – Chetan Lodhi
      Jan 27 '17 at 6:47














    1












    1








    1







    Instead of accessing the text from the cell's label you need to access the array that you have used to fill the tableView's cell data.



    So you need to use UITableViewDelegate method didSelectRowAtindexPath and access the array that you are using with your tableView methods.



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
    //Access the array that you have used to fill the tableViewCell
    print(yourArray[indexPath.row])
    }


    Note: Once confirm that TableView delegate is properly connected with your ViewController so that it will call didSelectRowAt method when you select cell.



    Edit: If you want to pass the data then try like this way.



    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showPlayer", sender: indexPath) //Pass indexPath as sender instead of self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPlayer" {

    let playerVC = segue.destination as! PlayerViewController
    let indexPath = sender as! IndexPath
    playerVC.trackName = sections[indexPath.section].items[indexPath.row]
    }
    }





    share|improve this answer















    Instead of accessing the text from the cell's label you need to access the array that you have used to fill the tableView's cell data.



    So you need to use UITableViewDelegate method didSelectRowAtindexPath and access the array that you are using with your tableView methods.



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
    //Access the array that you have used to fill the tableViewCell
    print(yourArray[indexPath.row])
    }


    Note: Once confirm that TableView delegate is properly connected with your ViewController so that it will call didSelectRowAt method when you select cell.



    Edit: If you want to pass the data then try like this way.



    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showPlayer", sender: indexPath) //Pass indexPath as sender instead of self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPlayer" {

    let playerVC = segue.destination as! PlayerViewController
    let indexPath = sender as! IndexPath
    playerVC.trackName = sections[indexPath.section].items[indexPath.row]
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 27 '17 at 6:25

























    answered Jan 27 '17 at 5:55









    Nirav DNirav D

    56.3k9103139




    56.3k9103139













    • Actually i have 5 sections and in all section there is diff. type of array elements(string). Thats why i'm using text label of the cell for the next view controller to play my sound file.

      – Chetan Lodhi
      Jan 27 '17 at 6:12











    • @Chetan Check the edited answer once to pass the data

      – Nirav D
      Jan 27 '17 at 6:25











    • Thanks a lot. This code is working. You saved my life. :) @ Nirav D

      – Chetan Lodhi
      Jan 27 '17 at 6:32













    • @Chetan welcome mate :)

      – Nirav D
      Jan 27 '17 at 6:33











    • Accepted you answer. :)

      – Chetan Lodhi
      Jan 27 '17 at 6:47



















    • Actually i have 5 sections and in all section there is diff. type of array elements(string). Thats why i'm using text label of the cell for the next view controller to play my sound file.

      – Chetan Lodhi
      Jan 27 '17 at 6:12











    • @Chetan Check the edited answer once to pass the data

      – Nirav D
      Jan 27 '17 at 6:25











    • Thanks a lot. This code is working. You saved my life. :) @ Nirav D

      – Chetan Lodhi
      Jan 27 '17 at 6:32













    • @Chetan welcome mate :)

      – Nirav D
      Jan 27 '17 at 6:33











    • Accepted you answer. :)

      – Chetan Lodhi
      Jan 27 '17 at 6:47

















    Actually i have 5 sections and in all section there is diff. type of array elements(string). Thats why i'm using text label of the cell for the next view controller to play my sound file.

    – Chetan Lodhi
    Jan 27 '17 at 6:12





    Actually i have 5 sections and in all section there is diff. type of array elements(string). Thats why i'm using text label of the cell for the next view controller to play my sound file.

    – Chetan Lodhi
    Jan 27 '17 at 6:12













    @Chetan Check the edited answer once to pass the data

    – Nirav D
    Jan 27 '17 at 6:25





    @Chetan Check the edited answer once to pass the data

    – Nirav D
    Jan 27 '17 at 6:25













    Thanks a lot. This code is working. You saved my life. :) @ Nirav D

    – Chetan Lodhi
    Jan 27 '17 at 6:32







    Thanks a lot. This code is working. You saved my life. :) @ Nirav D

    – Chetan Lodhi
    Jan 27 '17 at 6:32















    @Chetan welcome mate :)

    – Nirav D
    Jan 27 '17 at 6:33





    @Chetan welcome mate :)

    – Nirav D
    Jan 27 '17 at 6:33













    Accepted you answer. :)

    – Chetan Lodhi
    Jan 27 '17 at 6:47





    Accepted you answer. :)

    – Chetan Lodhi
    Jan 27 '17 at 6:47













    3














    This is working for me:



        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath) as! YourCellType
    let labelContent = cell.labelToAccess.text
    }


    First line creates an instance of your cell type (or standard cell, not my case).



    The second saves the content of your label (labelToAccess in this case) in a constant.






    share|improve this answer
























    • Thanks for reply.

      – Chetan Lodhi
      Oct 3 '17 at 6:05
















    3














    This is working for me:



        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath) as! YourCellType
    let labelContent = cell.labelToAccess.text
    }


    First line creates an instance of your cell type (or standard cell, not my case).



    The second saves the content of your label (labelToAccess in this case) in a constant.






    share|improve this answer
























    • Thanks for reply.

      – Chetan Lodhi
      Oct 3 '17 at 6:05














    3












    3








    3







    This is working for me:



        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath) as! YourCellType
    let labelContent = cell.labelToAccess.text
    }


    First line creates an instance of your cell type (or standard cell, not my case).



    The second saves the content of your label (labelToAccess in this case) in a constant.






    share|improve this answer













    This is working for me:



        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath) as! YourCellType
    let labelContent = cell.labelToAccess.text
    }


    First line creates an instance of your cell type (or standard cell, not my case).



    The second saves the content of your label (labelToAccess in this case) in a constant.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 2 '17 at 21:29









    Jorge SánchezJorge Sánchez

    674




    674













    • Thanks for reply.

      – Chetan Lodhi
      Oct 3 '17 at 6:05



















    • Thanks for reply.

      – Chetan Lodhi
      Oct 3 '17 at 6:05

















    Thanks for reply.

    – Chetan Lodhi
    Oct 3 '17 at 6:05





    Thanks for reply.

    – Chetan Lodhi
    Oct 3 '17 at 6:05











    1














    Try this 'Swift 4' -



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let currentCellTxt = yourTableView.cellForRow(at: indexPath)! as? YourCustomTableViewCell

    print(currentCellTxt?.lblYourName?.text) // 'lblYourName' that you defined in your 'YourCustomTableViewCell'

    }





    share|improve this answer





















    • 3





      Um...you already have indexPath in the function parameter of didSelectRowAtIndexPath...why would you even bother getting indexPath again? I am border line wanting to down vote.

      – Zhang
      Jan 27 '17 at 5:59











    • This is not working. @iDeveloper. Thanks for reply.

      – Chetan Lodhi
      Jan 27 '17 at 6:12
















    1














    Try this 'Swift 4' -



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let currentCellTxt = yourTableView.cellForRow(at: indexPath)! as? YourCustomTableViewCell

    print(currentCellTxt?.lblYourName?.text) // 'lblYourName' that you defined in your 'YourCustomTableViewCell'

    }





    share|improve this answer





















    • 3





      Um...you already have indexPath in the function parameter of didSelectRowAtIndexPath...why would you even bother getting indexPath again? I am border line wanting to down vote.

      – Zhang
      Jan 27 '17 at 5:59











    • This is not working. @iDeveloper. Thanks for reply.

      – Chetan Lodhi
      Jan 27 '17 at 6:12














    1












    1








    1







    Try this 'Swift 4' -



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let currentCellTxt = yourTableView.cellForRow(at: indexPath)! as? YourCustomTableViewCell

    print(currentCellTxt?.lblYourName?.text) // 'lblYourName' that you defined in your 'YourCustomTableViewCell'

    }





    share|improve this answer















    Try this 'Swift 4' -



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let currentCellTxt = yourTableView.cellForRow(at: indexPath)! as? YourCustomTableViewCell

    print(currentCellTxt?.lblYourName?.text) // 'lblYourName' that you defined in your 'YourCustomTableViewCell'

    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 26 '18 at 6:29

























    answered Jan 27 '17 at 5:54









    iDeveloperiDeveloper

    9941523




    9941523








    • 3





      Um...you already have indexPath in the function parameter of didSelectRowAtIndexPath...why would you even bother getting indexPath again? I am border line wanting to down vote.

      – Zhang
      Jan 27 '17 at 5:59











    • This is not working. @iDeveloper. Thanks for reply.

      – Chetan Lodhi
      Jan 27 '17 at 6:12














    • 3





      Um...you already have indexPath in the function parameter of didSelectRowAtIndexPath...why would you even bother getting indexPath again? I am border line wanting to down vote.

      – Zhang
      Jan 27 '17 at 5:59











    • This is not working. @iDeveloper. Thanks for reply.

      – Chetan Lodhi
      Jan 27 '17 at 6:12








    3




    3





    Um...you already have indexPath in the function parameter of didSelectRowAtIndexPath...why would you even bother getting indexPath again? I am border line wanting to down vote.

    – Zhang
    Jan 27 '17 at 5:59





    Um...you already have indexPath in the function parameter of didSelectRowAtIndexPath...why would you even bother getting indexPath again? I am border line wanting to down vote.

    – Zhang
    Jan 27 '17 at 5:59













    This is not working. @iDeveloper. Thanks for reply.

    – Chetan Lodhi
    Jan 27 '17 at 6:12





    This is not working. @iDeveloper. Thanks for reply.

    – Chetan Lodhi
    Jan 27 '17 at 6:12











    0














    You didn't set any text for textLabel. Hence it returns nil value.



    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell



    let imageNames = sections[indexPath.section].images[indexPath.row]

    cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
    cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
    cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
    //cell.detailTextLabel?.textColor = UIColor.red
    //cell.isHighlighted = false

    cell.backgroundColor = UIColor.clear
    cell.iconview.image = UIImage(named: imageNames)

    return cell


    }



    In didSelectRow func try to get value from Labeltittle.text not from textLabel.text



    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)!

    celltext = currentCell.Labeltittle.text
    performSegue(withIdentifier: "showPlayer", sender: self)


    }






    share|improve this answer




























      0














      You didn't set any text for textLabel. Hence it returns nil value.



      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell



      let imageNames = sections[indexPath.section].images[indexPath.row]

      cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
      cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
      cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
      //cell.detailTextLabel?.textColor = UIColor.red
      //cell.isHighlighted = false

      cell.backgroundColor = UIColor.clear
      cell.iconview.image = UIImage(named: imageNames)

      return cell


      }



      In didSelectRow func try to get value from Labeltittle.text not from textLabel.text



      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



      let indexPath = tableView.indexPathForSelectedRow!
      let currentCell = tableView.cellForRow(at: indexPath)!

      celltext = currentCell.Labeltittle.text
      performSegue(withIdentifier: "showPlayer", sender: self)


      }






      share|improve this answer


























        0












        0








        0







        You didn't set any text for textLabel. Hence it returns nil value.



        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell



        let imageNames = sections[indexPath.section].images[indexPath.row]

        cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
        cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
        cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
        //cell.detailTextLabel?.textColor = UIColor.red
        //cell.isHighlighted = false

        cell.backgroundColor = UIColor.clear
        cell.iconview.image = UIImage(named: imageNames)

        return cell


        }



        In didSelectRow func try to get value from Labeltittle.text not from textLabel.text



        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



        let indexPath = tableView.indexPathForSelectedRow!
        let currentCell = tableView.cellForRow(at: indexPath)!

        celltext = currentCell.Labeltittle.text
        performSegue(withIdentifier: "showPlayer", sender: self)


        }






        share|improve this answer













        You didn't set any text for textLabel. Hence it returns nil value.



        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell



        let imageNames = sections[indexPath.section].images[indexPath.row]

        cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
        cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
        cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
        //cell.detailTextLabel?.textColor = UIColor.red
        //cell.isHighlighted = false

        cell.backgroundColor = UIColor.clear
        cell.iconview.image = UIImage(named: imageNames)

        return cell


        }



        In didSelectRow func try to get value from Labeltittle.text not from textLabel.text



        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



        let indexPath = tableView.indexPathForSelectedRow!
        let currentCell = tableView.cellForRow(at: indexPath)!

        celltext = currentCell.Labeltittle.text
        performSegue(withIdentifier: "showPlayer", sender: self)


        }







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 27 '17 at 10:31









        Thirumal SivaThirumal Siva

        112




        112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41887698%2fhow-to-get-label-text-of-uitableview-cell-using-swift%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin