Tom
Tom

Reputation: 4033

Swift - How to get access of constant outside of if statement

My intention is to create different UITableViewCells for a dynamic UITableView. I've got the cells set up, I just need to add some logic which cell is to be displayed.

I decided to use an if statement which returns the cell. Somehow this is creating an error, since the func tableView needs to return 'cell' as well and code outside of the if statement doesn't have access to let cell = ...

How can I solve this problem?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let date = dateArray[indexPath.row]
        let title = titleArray[indexPath.row]
        let desc = descArray[indexPath.row]

        if notificationType == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TuvTavleViewCell") as! TuvTableViewCell

            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationViewCell") as! NotificationViewCell

            return cell
        }

        cell.setTitle(title: title)
        cell.setDesc(desc: desc)
        cell.setDate(date: date)

        return cell
    }

Upvotes: 0

Views: 77

Answers (2)

Mohmmad S
Mohmmad S

Reputation: 5088

Well, what you're asking is

How to get access of constant outside of if statement ?

This is actually not possible due to the scope modifier, anything declared within any type of closures loses it's scope when you get out of that closure.

So no you cant access anything declared inside an if statement out of that statement.

Now for your case how to solve the current problem, you have two options to do.

You can declare each cell out of the if or you can handle each one inside it's case.

You can even take this to another level by applying some protocols, and for that i will show you a simple example.

First of all you need this protocol and you need to confirm to it in your custom cells classes,

   protocol ConfigurableCell {
    func set(title: String)
    func set(desc: String)
    func set(date: Date)
   }

Confirming class TuvTableViewCell: ConfigurableCell and class NotificationViewCell: ConfigurableCell

Then you can do something like this.

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let date = dateArray[indexPath.row]
    let title = titleArray[indexPath.row]
    let desc = descArray[indexPath.row]
    var cell: ConfigurableCell

    if notificationType == 1  {
         cell = tableView.dequeueReusableCell(withIdentifier: "TuvTavleViewCell") as! ConfigurableCell

    } else {
         cell = tableView.dequeueReusableCell(withIdentifier: "NotificationViewCell") as! ConfigurableCell
    }
    cell.set(date: date)
    cell.set(desc: desc)
    cell.set(title: title)
}

As you see this will allow me to use the functions out side the if however according to the answer of your question i still didn't declared the cell inside the if, it's declared before.

Upvotes: 2

Shehata Gamal
Shehata Gamal

Reputation: 100533

You need

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let date = dateArray[indexPath.row]
    let title = titleArray[indexPath.row]
    let desc = descArray[indexPath.row]

    if notificationType == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TuvTavleViewCell") as! TuvTableViewCell
        cell.setTitle(title: title)
        cell.setDesc(desc: desc)
        cell.setDate(date: date)
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationViewCell") as! NotificationViewCell
        cell.setTitle(title: title)
        cell.setDesc(desc: desc)
        cell.setDate(date: date)
        return cell
    }

}

plus consider having a configure method inside the cell class and pass the model to it , Also instead of having 3 arrays consider having a struct model like

struct Item {
   let title,desc,date:String
}

Upvotes: 0

Related Questions