Yevgen
Yevgen

Reputation: 101

Value of type 'UITableViewCell' has no member 'name' in multiple TableView

I tried to make ViewController with two TableView but meet the problem.

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableTN: UITableView!
@IBOutlet weak var tableMainNews: UITableView!

var topnews: [TopNews]? = []
var mainnews: [Mainnewsfeed]? = []

override func viewDidLoad() {
    super.viewDidLoad()
    TopNewsJSON()
    MainNewsJSON()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:UITableViewCell?

    if tableView == self.tableTN {
        cell = tableView.dequeueReusableCell(withIdentifier: "topnewsCell", for:indexPath) as! TopNewsCell
        cell!.imgTN!.downloadImage(from: (self.topnews?[indexPath.item].image!)!)
        cell!.titleTN!.text = self.topnews?[indexPath.item].headline
    }

    if tableView == self.tableMainNews {
        cell = tableView.dequeueReusableCell(withIdentifier: "mainnewsCell", for:indexPath) as! MainNewsCell
        cell!.mainnews_title!.text = self.mainnews?[indexPath.item].headline
    }
    return cell!
}


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var count:Int?

    if tableView == self.tableTN {
        count = self.topnews!.count
    }

    if tableView == self.tableMainNews {
        count = self.mainnews!.count
    }

    return count!
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //print(indexPath)
}

func TopNewsJSON () {
     let urlRequest = URLRequest(url: URL(string: "https://sportarena.com/wp-api/topnews2018/top/")!)

     let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in

        if error != nil {
            print(error as Any)
            return
        }

        self.topnews = [TopNews]()
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]

            //print(json)
            let TN = TopNews()
            let jarray = json["top-news"] as! NSArray
            let jarray1 = jarray[0] as? [String: AnyObject]
            if let ID = jarray1!["ID"] as? String,
                let title =  jarray1!["title"] as? String,
                let img = jarray1!["img"] as? String {
                    TN.headline = title
                    TN.image = img
                    TN.id = ID
                }
                self.topnews?.append(TN)

                DispatchQueue.main.async {
                    self.tableTN.reloadData()
                }

        } catch let error {
            print(error)
        }
    }
    task.resume()
}

func MainNewsJSON () {
    let urlRequest = URLRequest(url: URL(string: "anyurl")!)

    let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in

        if error != nil {
            print(error as Any)
            return
        }
        //self.mainnews = [MainNews]()
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]

            let jarray = json["general-news"] as! NSArray
            let jarray1 = jarray[0]

            for jarray1 in jarray1 as! [[String: Any]] {
                let MNF = Mainnewsfeed()
                if let ID = jarray1["id"],
                    let title = jarray1["title"],
                    let time = jarray1["datetime"] {
                    MNF.headline = title as? String
                    MNF.id = ID as? String
                    MNF.time = time as? String
                }
                self.mainnews?.append(MNF)

                DispatchQueue.main.async {
                    self.tableMainNews.reloadData()
                }
            }

        } catch let error {
            print(error)
        }
    }
    task.resume()
}
}
}

After three lines as cell!.titleTN!.text = self.topnews?[indexPath.item].headline and others display error: "Value of type 'UITableViewCell' has no member 'titleTN'" (or also 'imgTN' and 'mainnews_title')

Where the error? What I need to change in my code?

Please help me.

Upvotes: 0

Views: 937

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100549

You can try

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView == self.tableTN {
        let cell = tableView.dequeueReusableCell(withIdentifier: "topnewsCell", for:indexPath) as! TopNewsCell
        cell.imgTN!.downloadImage(from: (self.topnews?[indexPath.item].image!)!)
        cell.titleTN!.text = self.topnews?[indexPath.item].headline
        return cell
    }
    else
    {
       let cell = tableView.dequeueReusableCell(withIdentifier: "mainnewsCell", for:indexPath) as! MainNewsCell
       cell.mainnews_title!.text = self.mainnews?[indexPath.item].headline
       return cell
    }

}

Upvotes: 1

Related Questions