ios 在自定义表视图中自定义图像

tyky79it  于 2023-01-22  发布在  iOS
关注(0)|答案(2)|浏览(242)

我想在Xcode中使用Swift为嵌套在自定义单元格和自定义表格视图中的图像添加圆角半径。这是我的代码。有人知道如何应用.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

n3schb8v

n3schb8v1#

在TableViewCell类文件中,

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

也可以在cellForRowAt中添加cornerRadius:

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

lsmepo6l2#

您可以在视图中添加imageview,然后

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

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

相关问题