How to fix 2 errors in code that occurred when indexPath and textLabel were put in

import UIKit
class MasterViewController: UITableViewController {
    var objects = [String]()
    var allWords = [String]()
    let object = objects[indexPath.row]
    cell.textLabel!.text = object

    override func awakeFromNib() {
        super.awakeFromNib()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        func startGame() {
            allWords.shuffle()
            title = allWords[0]
            objects.removeAll(keepCapacity: true)
            tableView.reloadData()
        }
        if let startWordsPath = NSBundle.mainBundle().pathForResource("start", ofType: "txt") {
            if let startWords = NSString(contentsOfFile: startWordsPath, usedEncoding: nil, error: nil) {
                allWords = startWords.componentsSeparatedByString("\n") as! [String]
            }
        } else {
            allWords = ["silkworm"]
        }
        startGame()
        /
        self.navigationItem.leftBarButtonItem = self.editButtonItem()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
    /

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }
}


This code that I copied and pasted into the MasterViewController.swift file is below:

let object = objects[indexPath.row]
    cell.textLabel!.text = object

The 2 errors that I get once I put the copied and pasted code into my project are use of unresolved identifier 'indexPath' for the line that starts with let object and expected declaration for cell.textlabel.

Thanks if anyone can help.

Best Regards,

-Comm.Dan

Accepted Answer

It looks like those two lines belong in a function (which has an indexPath parameter and a cell parameter) and not directly within your MasterViewController class.


Maybe this?

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
     let object = objects[indexPath.row]
     cell.textLabel!.text = object
}

Dear LCS,

Thank you so much for the help. I really appreciate it.

Best Regards,

-Comm.Dan

I suggest you should check table view tutorials so you get better understading how it works. For example why do you need to override those UITableViewController methods etc.

How to fix 2 errors in code that occurred when indexPath and textLabel were put in
 
 
Q