ios - 如何在函数的返回语句中插入 UIImage View ?

标签 ios swift uitableview

在我的这部分代码中,我试图在页脚中插入一个 UIImage 来分隔我的 2 个表格。返回部分上方的注释行是我尝试过但显示错误的代码。即使是 "let image:UIImage = UIImage(contentsOfFile: "white") 这行! return image” 我仍然无法让它工作。我收到的错误是“无法将类型为‘UIImage’的返回表达式转换为返回类型‘String?’”。我已将图像保存为“白色”和 png 格式。

func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    if section == 0 {
        if ftSubjs.count == 0 {
            return "No Classes Today"
        }
        else {
            //return UIImageView.init(image: "white")
            //return UIImageView.image = (resourceName: "white")
            //return UIImage(named: "white.png")
            //return "_"
            let image:UIImage = UIImage(contentsOfFile: "white")!

            return image
        }
    }
    else {
        if ptSubjs.count == 0 {
            return "No Classes Today"
        }
        else {
            return nil
        }
    }
}

*我试图在表格底部添加一个额外的空间,因此我使用了白色图像。我忘记了只需单击空格键就可以做到这一点。如果有人正在寻找这样的解决方案,请在 return 语句中添加一个空格,看起来像这样(保证它会起作用!):

            return " "

最佳答案

使用该方法返回UIImage

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let bgView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
    let infolabel = UILabel(frame: bgView.frame)
    infolabel.text = "No Classes Today"
    if section == 0 {
        if ftSubjs.count == 0 {
            return bgView.addSubview(infolabel)
        }
        else {
            //return UIImageView.init(image: "white")
            //return UIImageView.image = (resourceName: "white")
            //return UIImage(named: "white.png")
            //return "_"
            let image:UIImage = UIImage(contentsOfFile: "white")!
            return bgView.addSubview(image)
        }
    }
    else {
        if ptSubjs.count == 0 {
            return bgView.addSubview(infolabel)
        }
        else {
            return nil
        }
    }
}

关于ios - 如何在函数的返回语句中插入 UIImage View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58514181/

相关文章:

ios - 网页加载后事件指示器不隐藏

SwiftUI 如何使用 C# 中的 'Delegate-EventHandler-EventArgs' 方式将数据从子级传递到父级

ios - 替代 Swift 中的臭味全局变量?

ios - 由于弹跳而无法获得正确的 ScrollView 方向

ios - 在 Xamarin iOS 中拒绝位置权限时应用程序崩溃

ios - 协议(protocol)扩展无效 (Swift)

ios - 如何使用 UItableViewCell 重用标识符

ios - 我如何刷新特定部分?

ios - UIView 与 UITableviewcontroller Swift 顶部的标签

ios - 如何在 iOS 中设置 Tableview Cell 之间的空间?