Home > other >  Customizing image in Custom TableView
Customizing image in Custom TableView

Time:01-21

I want to add a corner radius to my image that is nested inside a custom cell, inside a custom table view using Swift in Xcode. Here is my code. Does anyone know how to apply .cornerRadius = 10?

struct Beer {
    let title: String
    let imageName: String
}

//Enter cell lines here for new beers
let data: [Beer] = [
    Beer(title: "Helles Half Life", imageName: "HellesHalfLife"),
    Beer(title: "Racemic Red Ale", imageName: "RedAle"),
    Beer(title: "RSIPA", imageName: "RSIPA"),
    Beer(title: "Stage II HAC", imageName: "HellesHalfLife"),
    Beer(title: "Helleva Stage II Lager", imageName: "HellevaStageII"),
    Beer(title: "Train of Four Stout", imageName: "TrainOfFour"),
    Beer(title: "Patrick's Feeling Hazy", imageName: "PatricksFeelingHazy"),
    Beer(title: "BIS 40", imageName: "BIS40"),
    Beer(title: "40 Winks", imageName: "FortyWinks"),
    Beer(title: "", imageName: ""),
]

override func viewDidLoad() {
    super.viewDidLoad()
    table.dataSource = self
    table.delegate = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let Beer = data[indexPath.row]
    let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!     TableViewCell
    cell.label.text = Beer.title
    cell.iconImageView.image = UIImage(named: Beer.imageName)
   return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->
CGFloat{
return 120
    }

I tried adding table.layer.cornerRadius = 10 in the override func viewDidLoad() section under table.delegate.

CodePudding user response:

in your TableViewCell Class File,

iconImageView.clipsToBounds = true
iconImageView.layer.cornerRadius = 10.0

or you can also add cornerRadius in cellForRowAt:

cell.iconImageView.image = UIImage(named: Beer.imageName)
cell.iconImageView.clipsToBounds = true
cell.iconImageView.layer.cornerRadius = 10.0

CodePudding user response:

You can add imageview inside a view and then

cell.yourImageViewContainerView.layer.cornerRadius = 10.0
cell.yourImageViewContainerView.clipsToBounds = true

or

cell.yourImageViewContainerView.layer.cornerRadius = 10.0
cell.yourImageView.clipsToBounds = true
  • Related