0

I have UITableView and the contect is Dynamic Prototype.

so I have 5 cells and each cell has it own identifier. so when I try to return the value in (cellForRowAt) it would let me.

would please help with that ?

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

    if (indexPath.section) == 0 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
        //  cell?.textLabel!.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]


        return cell!

    }
    else if (indexPath.section) == 2 {
        let cell3 = tableView.dequeueReusableCell(withIdentifier: "cellThree") as UITableViewCell!
        return cell3!
    }

    else if (indexPath.section) == 3 {
        let cell4 = tableView.dequeueReusableCell(withIdentifier: "cellFour") as UITableViewCell!
        return cell4!
    }

    else if (indexPath.section) == 4 {
        let cell5 = tableView.dequeueReusableCell(withIdentifier: "cellFive") as UITableViewCell!
        return cell5!
    }

    return cell!

}

Thanks !

New Update :-

so the error that is showing to me is (Use of unresolved identifier 'cell') so when I return at the end (return cell!) it shows this error.However, if I deleted that line it shows me another error asking me to return a value

so bottomline I'm confised what value should I return at the end of (cellForRowAt).

Nawaf
  • 45
  • 8

2 Answers2

0

EDIT: I just realized you aren't using the for indexPath parameter in dequeueReusableCellWithIdentifier. Use that, otherwise it's the wrong one. See:

@Nawaf you can't return multiple values from any function, hence you can't return multiple cells from one call of cellForRow. You may be misunderstanding how cellForRowAtIndexPath works. Right now your code is not populating anything in the cells.

Use of unresolved identifier 'cell'

Furthermore, your error can be occurring for multiple reasons. For one, check that your view controller is correctly assigned to the one in storyboard and that you have assigned a reuseIdentifier in storyboard.

See the links for more info: https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614861-tableview?language=objc

How does cellForRowAtIndexPath work?

Also a side note for code quality: don't force cast to the cell because that can cause unintended errors. Use conditional binding instead:

guard let cell = tableView.dequeueReusableCellWithIdentifier(...) as? UITableViewCell else { // What to do when the cast failed }

Good luck :)

Mihir Thanekar
  • 508
  • 4
  • 8
  • And what would you do when the cast failed? `cellForRow` is one of the cases where I would use a force cast; There is no sensible way of handing the failure at runtime and if you force downcast you will quickly crash during development and you can work out why the cell couldn't be found – Paulw11 Aug 08 '17 at 22:26
  • If the cast failed, you may have something wrong and thats where your analytics framework can report that something crashed given a live app. Then, you'd be able to check if that was one user and the crash wasn't on your end or see if there's an evident error that must be fixed.. etc. It's just a matter of preference. And yes.. during development it's easier to see the force crash = something wrong. – Mihir Thanekar Aug 08 '17 at 23:08
0

Return values

This method can only return one cell each time it's called. The method will be called each time a new cell is about to appear on the screen. Take a look at the method signature:

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

That UITableViewCell at the end is the expected return value and in this case it's a single cell.

The error

Let's take a look at your error:

Use of unresolved identifier 'cell'

"Unresolved identifier" means that it has no idea what "cell" is as it has not been declared in the current scope. But don't you declare cell in this method? Yes you do, but they are in a separate scope. Variable scope defines how long a variable lives and where it can be seen. You can tell when a new variable scope is declared when you see a new set of curly brackets {}, which each of your if statements declare. Variables declared within each set of curly brackets can only be seen by code contained within those brackets. Once execution leaves those brackets, those variables are gone. Let's take a look at what variables are viewable in the scope of your final return statement by removing the scopes created by your if statements:

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


    return cell!
}

Now it is clear that cell doesn't exist at the point of the return. You'll need to declare cell in the method's scope and give it some value. Please note that force unwrapping your cell optionals will likely lead to a crash at runtime as the method must not return nil. You'll want to create cells when tableView.dequeueReusableCell() returns nil.

BergQuester
  • 6,167
  • 27
  • 39