Swift TableViewCell Image 就像 iPhone 上的设置应用程序

问题描述 投票:0回答:1

问题:

我想将设置应用程序左侧 cell.UIImage 与 sfSymbols 复制到 T 以编程方式不使用笔尖或自定义单元格。

我正在尝试复制单元格左侧的设置应用程序 sfSymbol 图像(如图所示)。

是否可以在不创建自定义单元格而仅使用 TableViewCell 的情况下执行此操作?

我遇到的问题是 imageView 背景颜色边框,同时调整其中 sfSymbol 的大小。问题是创建 sfSymbol 以适应背景,就像设置应用程序一样。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        
        
        cell?.textLabel?.font = .systemFont(ofSize: 17, weight: .regular)
        cell?.accessoryType = .disclosureIndicator
        cell?.selectionStyle = .none
        
        let item = tableItems[indexPath.row]
        cell?.textLabel?.text = item.title

         //let config = UIImage.SymbolConfiguration(pointSize: 25, weight: .regular, scale: .small)
         //cell?.imageView?.image = UIImage(systemName: item.systemImageName, withConfiguration: config)?.withTintColor(.white, renderingMode: .alwaysOriginal)
        
        
        cell?.imageView?.image = UIImage(systemName: item.systemImageName)?.withTintColor(.white, renderingMode: .alwaysOriginal)

        cell?.imageView?.backgroundColor = tableItems[indexPath.row].backgroundColor
       
        if let imageView = cell?.imageView, imageView.transform == .identity {
           imageView.transform = imageView.transform.scaledBy(x: 0.50, y: 0.50)
           imageView.contentMode = .center
           imageView.layer.cornerRadius = 5
        }

        
        return cell!
    }

我的目标是在不创建自定义单元或笔尖的情况下复制此内容。也许以编程方式创建自定义容器视图。

输出(我的代码是什么样的):我需要它来复制上图中的设置应用程序图像。

ios swift uitableview tableview cell
1个回答
0
投票

复制设置应用程序图标(sfSymbols),无需笔尖或自定义单元格。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        
        
        cell?.textLabel?.font = .systemFont(ofSize: 17, weight: .regular)
        cell?.accessoryType = .disclosureIndicator
        cell?.selectionStyle = .none
        
        let item = tableItems[indexPath.row]
        cell?.textLabel?.text = item.title
        
        
        // Create a custom UIImageView for the SF Symbol
        let config = UIImage.SymbolConfiguration(pointSize: 15, weight: .regular, scale: .small)
        let imageView = UIImageView(image: UIImage(systemName: item.systemImageName,withConfiguration: config)?.withTintColor(.white, renderingMode: .alwaysOriginal))
        imageView.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
        imageView.contentMode = .center
        
        // Set the background color and corner radius
        imageView.backgroundColor = tableItems[indexPath.row].backgroundColor
        imageView.layer.cornerRadius = 5
        
        // Convert the custom UIImageView to UIImage
        if let compositeImage = image(from: imageView) {
            // Set the custom image as the cell's imageView image
            cell?.imageView?.image = compositeImage
        }
        
        return cell!
    }


func image(from view: UIView) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
        if let context = UIGraphicsGetCurrentContext() {
            view.layer.render(in: context)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
        return nil
    }
© www.soinside.com 2019 - 2024. All rights reserved.