我想添加一个角半径到我的图像是嵌套在一个自定义单元格,在一个自定义表视图内使用Swift在Xcode。这是我的代码。有人知道如何应用.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
}
我尝试在table.delegate
下的override func viewDidLoad()
部分添加table.layer.cornerRadius = 10
。
在TableViewCell类文件中,
iconImageView.clipsToBounds = true
iconImageView.layer.cornerRadius = 10.0
或者你也可以在cellForRowAt:
中添加cornerRadiuscell.iconImageView.image = UIImage(named: Beer.imageName)
cell.iconImageView.clipsToBounds = true
cell.iconImageView.layer.cornerRadius = 10.0
你可以在视图中添加imageview,然后
cell.yourImageViewContainerView.layer.cornerRadius = 10.0
cell.yourImageViewContainerView.clipsToBounds = true
或
cell.yourImageViewContainerView.layer.cornerRadius = 10.0
cell.yourImageView.clipsToBounds = true