我有下面的代碼來配置表格視圖中單元格的內容和外觀。它按預期作業,但我想在影像周圍添加一個淺灰色邊框。
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
}
在代碼的其他地方,我可以使用類似的東西來做到這一點
flag.layer.borderColor = UIColor.lightGray.cgColor
flag.layer.borderWidth = 1
其中標志是 UIImageView。但是,我無法弄清楚如何在細胞內做類似的事情。我嘗試過使用 cell.backgroundConfiguration,但這會影響整個單元格,而不僅僅是影像。誰能給我一個提示?提前致謝
uj5u.com熱心網友回復:
什么是內容配置?這是一種構建視圖(“內容視圖”)的方式,以及修改該視圖的一系列方法,通過“配置狀態”進行通信:
public protocol UIContentConfiguration {
func makeContentView() -> UIView & UIContentView
func updated(for state: UIConfigurationState) -> Self
}
您已經購買了現有的內容配置。因此,您獲得的視圖就是您獲得的視圖(一個標簽和一個影像視圖),您可以進行的修改是您可以進行的修改(由配置狀態定義)。
如果你想改變它,你必須在那個架構的某個地方提供一個自定義物件。您可以制作自己的內容配置,繪制單元格內容并在影像視圖周圍放置邊框。或者,您可能能夠擺脫修改狀態,如 UIConfigurationStateCustomKey 檔案中所述。但關鍵是,您不能只使用默認配置并期望能夠直接修改子視圖,因為(正如您所發現的)您無權訪問子視圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515076.html
