I have the code below for configuring the content and look of cells in a table view. It works as intended, but I'd like to add a light gray border around the image.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
var cellConfig = cell.defaultContentConfiguration()
let cellImage = UIImage(named: "Flags/" pictures[indexPath.row])?.resize(withSize: CGSize(width: 50, height: 25))
cellConfig.image = cellImage
cellConfig.text = countries[indexPath.row]
cell.contentConfiguration = cellConfig
return cell
}
Elsewhere in the code, I'm able to do that using something like
flag.layer.borderColor = UIColor.lightGray.cgColor
flag.layer.borderWidth = 1
where flag is a UIImageView. However, I cannot figure out how to do something analogous inside a cell. I've tried playing with cell.backgroundConfiguration, but that affects the entire cell, not just the image. Can anyone give me a hint? Thanks in advance
CodePudding user response:
What is a content configuration? It's a way of constructing a view (a "content view"), along with a repertoire of ways to modify that view, as communicated by way of a "configuration state":
public protocol UIContentConfiguration {
func makeContentView() -> UIView & UIContentView
func updated(for state: UIConfigurationState) -> Self
}
You have bought into an existing content configuration. Therefore the views you get are the views you get (one label and one image view), and the modifications you can make are the modifications you can make (as defined by the configuration state).
If you want to change that, you have to provide a custom object somewhere in that architecture. You can make up your own content configuration which draws the cell contents and puts the border around the image view. Alternatively, you might be able to get away with modifying the state, as explained in the documentation of UIConfigurationStateCustomKey. But the point is, you can't just use the default configuration and expect to be able to modify the subviews directly, because (as you've discovered) you've no access to the subviews.
CodePudding user response:
Try the below code -
cellConfig.image.layer.borderColor = UIColor.lightGray.cgColor
cellConfig.image.layer.borderWidth = 1