0

I have a struct to store name and image names of pictures which are already bundled in app as follows

struct City { var name: String
              var imageName: String }

class firstViewController: UIViewController

let cities = [City(name:"City00", imageName:"city_00"),
              City(name:"City01", imageName:"city_01"),
              City(name:"City02", imageName:"city_02")]

In displayViewController, I have a variable nowCityImages to store data selected from firstViewcontroller

class displayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

var nowCityImages: [String] = []

Now I want to display image of city in tableview

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

    let outCityImages = nowCityImages[indexPath.item]

    // This is not displaying any images
     myCell.cityImage.image = UIImage(named:outCityImages)

   return myCell
}

If I use print(outCityImages), I am getting following output as excepted

City00
City02

but I don't know how to replace City00 with city_00 and City02 with city_02 in tableview cell so that it displays the image.

3
  • This can help you: stackoverflow.com/questions/25665122/… Commented Dec 23, 2016 at 8:12
  • @JimmyJames, that question is for searching, I am looking for substituting Commented Dec 23, 2016 at 8:15
  • 1
    Check the statement where you add or append strings to nowCityImages. It should be similar to city.imageName not city.name. e.g. nowCityImages.append(cities[0].imageName) Commented Dec 23, 2016 at 8:16

2 Answers 2

0

if the number at the end is 2 digit length:

var str = "City00"
let index = str.index(str.endIndex, offsetBy: -2)

var str1 = str.substring(to: index).lowercased()
var str2 = str.substring(from: index)
let name = "\(str1)_\(str2)" //city_00
Sign up to request clarification or add additional context in comments.

2 Comments

That is not always the case, I just gave an example to spell out my problem.I like to know how to substitute corresponding names of cities I am getting with their image names.
In displayViewController, there is the city name only. I think you might pass City object/imageName to displayViewController rather than the city name only or create a method to get city object by city name.
0

Can you change to code of getting nowCityImages to this

    var nowCityImages: [String] = cities.filter(){$0.imageName}

Now it will return image name as you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.