Rome Torres
Rome Torres

Reputation: 1091

iOS swift 3 how can I change types in a switch statement

I have 2 TableViewCells that upload images and the code is essentially identical . What I want to do is get that code and get it into 1 function so that I can reduce duplication . I am however having trouble casting things properly . The 2 tableview Cells are called HomeTVC and ProfileTVC and they both have an UiImageView which is named profile_Image .

Here is how I call that function

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC

 homeProfile.sharedGetImage(cellType: cell,streamsModel: streamsModel, row: indexPath.row)

}

Obviously the code above belongs to the HomeTVC tableView, now here is the function

   func sharedGetImage (cellType: UITableViewCell?,streamsModel: streamModel, row: Int) {

        var cell = HomeTVC()

        switch cellType {

        case is HomeTVC :
         cell =  HomeTVC()
         break

        case  is ProfileTVC :
        cell = cell as! ProfileTVC

        default : break

        }


        if streamsModel.profile_image_string[row] != "" {

            if let image = streamsModel.imageCache.object(forKey: streamsModel.profile_image_string[row] as AnyObject) as? UIImage {
                cell.profile_image.image = image
            }

          }
     }

I'm getting an error inside the switch statement on cell = cell as! ProfileTVC the error is cast from HomeTVC to ProfileTVC fails unrelated type . Which I understand, how can I get around that issue ? What I want to do is detect what type the UITableViewCell is then get a variable and change it to that type so that I can access the profile_image property .

Upvotes: 1

Views: 604

Answers (1)

nathangitter
nathangitter

Reputation: 9787

In your sharedGetImage function, you are passing a reference to the table view cell, so use that instead of creating a new cell.

func sharedGetImage(cellType: UITableViewCell?, streamsModel: streamModel, row: Int) {

    if let cell = cellType as? HomeTVC {
        if streamsModel.profile_image_string[row] != "" {
            if let image = streamsModel.imageCache.object(forKey: streamsModel.profile_image_string[row] as AnyObject) as? UIImage {
                cell.profile_image.image = image
            }
        }
    }

    if let cell = cellType as? ProfileTVC {
        if streamsModel.profile_image_string[row] != "" {
            if let image = streamsModel.imageCache.object(forKey: streamsModel.profile_image_string[row] as AnyObject) as? UIImage {
                cell.profile_image.image = image
            }
        }
    }

}

Alternatively, when you have two classes that have the same property, this is a great chance to use a protocol. You could create a protocol to specify that an object has a UIImageView for a profile image.

protocol ProfileImageDisplaying {
    var profileImageView: UIImageView
}

You could make both cells adopt this protocol, and then you would only need to do one check (to see if the cell is ProfileImageDisplaying) instead of two.

Upvotes: 2

Related Questions