Home>

I am creating an iOS app using the swift language with xCode.
Do not place anything on StoryBoard, just create a ViewController,
I want to implement the contents only with code.

We received a response from takabosoft, the code was corrected, and the error was resolved.
However, I am trying to set it, but neither text nor images are displayed.
Thanks for your cooperation.

・ Image of each cell I want to create
---------------
Name: 〇〇 | Display image
---------------

Error message

Nothing is displayed.

Applicable source code
import UIKit
class CustomTableViewCell: UITableViewCell {
    var id = UILabel ()
    var icon = UIImageView ()
    override func awakeFromNib () {
        super.awakeFromNib ()
        // Initialization code
    }
    override func setSelected (_ selected: Bool, animated: Bool) {
        super.setSelected (selected, animated: animated)
        // Configure the view for the selected state
    }
    func setCell (idList: IDList) {
        self.id.text = idList.id
        self.icon.image = UIImage (named: idList.imgName)
    }
}
import Foundation
// Model for ID selection screen
class IDList: NSObject {
    var id: String!
    var imgName: String!
    init (sId: String, imgName: String!) {
        self.id = sId
        self.imgName = imgName
    }
}
* Excerpt
    var testData: [IDList] = [IDList] ()
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Get the screen size
        let iWidthScreen = self.view.frame.size.width
        let iHeightScreen = self.view.frame.size.height
        // title
        let lblTitle = UILabel ()
        lblTitle.frame = CGRect (x: 0, y: 45, width: iWidthScreen, height: 20)
        lblTitle.text = "Title"
        lblTitle.textAlignment = NSTextAlignment.center
        self.view.addSubview (lblTitle)
        // ID list
        let tableView = UITableView ()
        tableView.frame = CGRect (x: 5, y: iHeightScreen * 0.15, width: iWidthScreen-10, height: iHeightScreen * 0.7)
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview (tableView)
        // Done button
        let btnConplete = UIButton ()
        btnConplete.frame = CGRect (x: iWidthScreen * 0.1, y: iHeightScreen * 0.9, width: iWidthScreen * 0.8, height: 50)
        btnConplete.setTitle ("Button 1")
        btnConplete.layer.cornerRadius = 12
        btnConplete.addTarget (self, action: #selector (btnNext_touched), for: .touchUpInside)
        self.view.addSubview (btnConplete)
}
    // TableView display quantity
    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        return testData.count
    }
// Set value in cell
Func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)->UITableViewCell {
        let cell: CustomTableViewCell = tableView.dequeueReusableCell (withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
        cell.id.frame = CGRect (x: 0, y: 0, width: 150, height: 30)
        cell.id.text = testData [indexPath.row] .id
        cell.imgName.image = UIImage (named: testData [indexPath.row] .imgName
        return cell
    }
Supplemental information (FW/tool version etc.)

・ Mac
・ XCode 10.1
・ Swift 3

  • Answer # 1

      

    Custom TableView class

    It's a UITableViewCell custom class, not UITableView.

    In the middle of ViewController's viewDidLoad

      

    tableView.register (CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")

    If you execute

    etc., a custom class for the cell will be generated.