ios - 以编程方式在 UITableViewCell 下添加 UITextView 和 UIImageView Swift 4

标签 ios swift uitableview uiimageview uitextview

我想在 UITableViewCell 下添加一张图片和一些文本。这是我想要达到的结果:

Here is what I want to achieve

但是我只能用纯代码实现UITableViewCell。有人知道如何在 UITableViewCell 下方或上方添加图片或一些文本吗?任何建议,将不胜感激。这是我当前的结果:

My current result

这是我的代码:

import UIKit

class AboutViewController : UITableViewController {
    private let about = ["About us"]
    private let termsOfService = ["Terms of service"]
    private let privacyPolicies = ["Privacy policies"]
    private let openSourceLicense = ["Open Source Libraries"]
    private let sections = ["about", "termsOfService", "privacyPolicies", "openSourceLicense"]

    let myTableView: UITableView = {
        let mt = UITableView()
        return mt
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        //         register cell name
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        // set DataSource
        myTableView.dataSource = self
        // set Delegate
        myTableView.delegate = self
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView.rowHeight
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            print("Value: \(about[indexPath.row])")
        } else if indexPath.section == 1 {
            print("Value: \(termsOfService[indexPath.row])")
        } else if indexPath.section == 2 {
            print("Value: \(privacyPolicies[indexPath.row])")
        } else if indexPath.section == 3 {
            print("Value: \(openSourceLicense[indexPath.row])")
        }
    }

    // return the number of cells each section.

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return about.count
        } else if section == 1 {
            return termsOfService.count
        } else if section == 2 {
            return privacyPolicies.count
        } else if section == 3 {
            return openSourceLicense.count
        } else {
            return 0
        }
    }

    // return cells

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "MyCell")

        if indexPath.section == 0 {
            cell.accessoryType = .disclosureIndicator
            cell.textLabel?.textAlignment = .left
            cell.textLabel?.text = "\(about[indexPath.row])"
        } else if indexPath.section == 1 {
            cell.accessoryType = .disclosureIndicator
            cell.textLabel?.textAlignment = .left
            cell.textLabel?.text = "\(termsOfService[indexPath.row])"
        } else if indexPath.section == 2 {
            cell.accessoryType = .disclosureIndicator
            cell.textLabel?.textAlignment = .left
            cell.textLabel?.text = "\(privacyPolicies[indexPath.row])"
        } else if indexPath.section == 3 {
            cell.accessoryType = .disclosureIndicator
            cell.textLabel?.textAlignment = .left
            cell.textLabel?.text = "\(openSourceLicense[indexPath.row])"
        }

        return cell
    }
}

最佳答案

感谢@Larme 的所有帮助。添加如下代码就可以给header添加一个UIImage(也就是说实现一个TableHeaderView):

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {


        let logoView = UIImageView()
        if section == 0 {
            logoView.contentMode = .scaleAspectFit
            let logo: UIImage = #imageLiteral(resourceName: "IMG_0517")
            logoView.image = logo
            view.addSubview(logoView)
            return logoView
        } else {
           return nil
        }



    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
           return 200
        } else {
            return tableView.rowHeight
        }

    } 

最终结果如下:

Final result picture

如果您想在 UITableViewCell 下添加一些文本,您可以在最后部分添加页脚。

关于ios - 以编程方式在 UITableViewCell 下添加 UITextView 和 UIImageView Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46569492/

相关文章:

iOS SimpleDB 按日期选择

swift - 如何将元组加在一起

objective-c - 如何在 Swift 中覆盖 Objective-c 类函数?

ios - 重用 UITableView 中的单元格

ios - UITableViewCell 分隔符在 iOS7 中消失

iphone - 如何让 UITableView(Controller) 捕获多点触控事件?

ios - 如何每天更新一次 UILabel

ios - 在 ViewController 中请求通知。

ios - touchesBegan 语法错误 - 没有成员

Swift:如何触发Xcode项目中的函数?