How can I get the text of a label using the new UIContentConfiguration

Am trying to port away from .textLabel.text, as per Apple but I can't seem to find the way to get the text I set.

I now set my cells this way:

let cell = UITableViewCell()
var config = cell.defaultContentConfiguration()
		
config.text = dbClass.nameList[indexPath.row].clientName
config.textProperties.color = .black
config.textProperties.alignment = .center
cell.contentConfiguration = config

But when I try to get the text of a selected cell (once the user hits the OK button) I can't seem to reverse engineer how to get the text.

let cell = myClientList.cellForRow(at: myInvsList.indexPathForSelectedRow!)
let config = cell?.contentConfiguration
dbClass.curMasterinvList = ?????? (can't find what property to read here

I even tried

let config = cell?.defaultContentConfiguration()

Hoping that the text would be in there, but the text there is blank, so am assuming that's just the standard config before I've changed it.

I have Googled as much as possible, but can't seem to find this very simple need.

Answered by Claude31 in 720274022

contentConfiguration is generic, with no text property.

UIListContentConfiguration is a subclass for cell configuration, which has a text property.

Doesn't this work ?

if let config = cell?.contentConfiguration {
    dbClass.curMasterinvList = config.text
}

No,

let config = cell?.contentConfiguration
dbClass.curMasterinvList = config?.text

Value of type 'UIContentConfiguration' has no member 'text'

I jumped to the definition in Xcode and found the following:

    @available(iOS 14.0, tvOS 14.0, *)
    public var contentConfiguration: UIContentConfiguration?

    @available(iOS 14.0, tvOS 14.0, *)
    public func defaultContentConfiguration() -> UIListContentConfiguration 

So I tried a casting the contentConfiguration as a UIListContentConfiguration and it works.

While it works, I would appreciate it if someone would tell me if I am still somehow doing it incorrectly. Perhaps I am configuring the cell incorrectly to begin with?

Here is the following working code:

let cell = myClientList.cellForRow(at: myInvsList.indexPathForSelectedRow!)
let config = cell?.contentConfiguration as? UIListContentConfiguration
dbClass.curMasterinvList = config?.text ?? ""
Accepted Answer

contentConfiguration is generic, with no text property.

UIListContentConfiguration is a subclass for cell configuration, which has a text property.

How can I get the text of a label using the new UIContentConfiguration
 
 
Q