0

This article was referenced to solve this problem. But I have successfully connected through segue but failed to get data.

link >> Cannot assign value of type String to type UILabel

I am working by embeding TableViewController after putting ContainerView in ViewController.

As I mentioned in the first problem, the connection is successful through segue, but the value is not passed. Which is the problem?

Last time I solved another type of problem, I succeeded, but this time it fails for some reason. I don't know. Any help would be appreciated.

ViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "FreeSegue" {
            let filteredBoard = BoardValue?.filter{$0.b_type == "1"}
            let mainPageTableVC = segue.destination as? MainPageTableVC
            mainPageTableVC?.boards = filteredBoard
            }
                        
        else if segue.identifier == "GameSegue" {
            let filteredBoard = BoardValue?.filter{$0.b_type == "2"}
            let mainPageTableVC = segue.destination as? MainPageTableVC
            mainPageTableVC?.boards = filteredBoard
        }
        
        else if segue.identifier == "FoodSegue" {
            let filteredBoard = BoardValue?.filter{$0.b_type == "3"}
            let mainPageTableVC = segue.destination as? MainPageTableVC
            mainPageTableVC?.boards = filteredBoard
        }
        
        else if segue.identifier == "CodingSegue" {
           let filteredBoard = BoardValue?.filter{$0.b_type == "4"}
            let mainPageTableVC = segue.destination as? MainPageTableVC
            mainPageTableVC?.boards = filteredBoard
        }
    }

MainPageTableVC

import UIKit

class MainPageTableVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var boards: [BoardList]?
    
    @IBOutlet var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource = self
    
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
            
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainPageViewCell", for: indexPath) as! MainPageTableViewCell
        
        if let boards = self.boards {
        let model = boards[indexPath.row]
            cell.txtTitle = model.b_title // ERROR [Cannot assign value of type 'String?' to type 'UILabel?']
            cell.txtUserId = model.userId // ERROR [Cannot assign value of type 'String?' to type 'UILabel?']
            cell.txtConut = model.b_count // ERROR [Cannot assign value of type 'Int?' to type 'UILabel?'
            cell.txtSaveTime = model.b_date // ERROR [Cannot assign value of type 'Int?' to type 'UILabel?']
            cell.txtComment = model.commentCount // ERROR [Cannot assign value of type 'Int?' to type 'UILabel?']
        }
        return cell
    }

jsonData

struct BoardList: Codable {
    var b_id: Int? // no used
    var b_recomment_id: Int? // no used
    var b_type: String?
    var b_title: String?
    var b_date: Int?
    var b_count: Int?
    var userId: String?
    var updateCheck: Int? // no used
    var commentCount: Int?
}

Simulator Executed Photographs (I drew a line because I thought it might be confusing.)

enter image description here

je2
  • 35
  • 1
  • 6
  • 1
    `cell.txtWHATEVER.text = ...` – Leo Dabus Oct 21 '20 at 05:26
  • I thought there was no problem, but I was too stupid to say that there was an error because the text was not attached to the back of the label. :'(....Thank you!!! – je2 Oct 21 '20 at 05:43

1 Answers1

1

You need to assign it to text property of UILabel

if let boards = self.boards {
let model = boards[indexPath.row]
    cell.txtTitle.text = model.b_title
    cell.txtUserId.text = model.userId
    cell.txtConut.text = String(model.b_count ?? 0)
    cell.txtSaveTime.text = String(model.b_date ?? 0)
    cell.txtComment.text = String(model.commentCount ?? 0)
 }
Mahendra
  • 8,448
  • 3
  • 33
  • 56