简体   繁体   中英

Add UITextView and UIImageView under UITableViewCell programmatically Swift 4

I would like to add one picture and some texts under UITableViewCell. Here is the result that I want to achieve:

这是我想要实现的

But I can only implement UITableViewCell with pure codes. Anyone know how to add a picture or some texts under or above the UITableViewCell? Any suggestions would be appreciated. Here is my current result:

我目前的结果

Here are my codes:

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
    }
}

Thanks @Larme for all the help. Add the following codes will be able to add a UIImage to the header (Which means to implement a 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
        }

    } 

Here is the final result shows:

Final result picture

If you would like to add some texts under the UITableViewCell, you can add a footer to the final section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM