1

I want to add different images to different cell in a tableView where i have already a list of string, this is my code, the struct of category:

  struct QCategoryy {
        var name:String
        var image:UIImage
        var isSelected = false
        init(name:String, image.UIImage) {
            self.name = name
            self.image = image
}


    extension QCategoryy: ExpressibleByStringLiteral {
        init(stringLiteral value: String) {
            self.name = value
        }
        init(unicodeScalarLiteral value: String) {
            self.init(name: value)
        }
        init(extendedGraphemeClusterLiteral value: String) {
            self.init(name: value)
        }
    }

and here is where i create the list (which i will then add to the tableView)

import Foundation
import UIKit
import CoreLocation
import Alamofire

class NearbyPlaces {
    static func getCategories() -> [QCategoryy] {
        let list:[QCategoryy] = ["Art_gallery", "Amusement_park", "Zoo", "Bar", "Night_club", "Movie_theater", "Restaurant", "Shopping_mall", "Atm", "Gym", "Store", "Spa", "Museum", "Stadium", "Hardware_store", "Casino", "Library", "Painter", "Book_store", "Bowling_alley", "Cafe", "Clothing_store",  ]
        return list
    }

for each item in the list i want to add a specific image of the cell size but how can i do?

EDIT

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "CATEGORY_CELL"
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        let selectedIndexPaths = tableView.indexPathsForSelectedRows
        let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
       /* cell.accessoryType = rowIsSelected ? .checkmark : .none  */
        cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none
        cell.textLabel?.text = list[indexPath.row].name
        return cell
    }

4 Answers4

0

The List ist static, right?

Why do you not add an image url (or what u need) to your object. That would solve your problem ^^. So you can call it in cell for row :)

TMX
  • 21
  • 2
  • yes the list is static, i can try but i don't know how to do –  Nov 07 '17 at 10:26
  • Just add a image or url , string (what ever..) to your QCategory. Add it to your init. I'm not sure with your extensions. Do you need them, are they required? If not: just change in your static to the follow: let list = [QCategory(name: "Your name" , image : "imageName"), ...] func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_identifier") let category = NearbyPlaces.getCategory()[indexPath.row]
    cell.image = category.image return cell!}
    – TMX Nov 07 '17 at 10:42
0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_identifier") 
let category = NearbyPlaces.getCategory()[indexPath.row] 
cell.image = category.image 
cell.name = category.name
return cell!

}

You cant read it in the comments^^

TMX
  • 21
  • 2
  • i tried, i edited the code if you see my QCategory struct now, but i get an error in the extension: "Declaration is only valid at file scope" –  Nov 07 '17 at 11:06
  • updated my anwer. you need to add at the time of initialization. – Rishi Chaurasia Nov 07 '17 at 11:20
0

As @TMX said, you can use:

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?

See: https://developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow

And: https://stackoverflow.com/a/8079765/7510062

And if you just started to code, you should follow this tutorial: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html

..in order to understand how it works, then it should be EASY!

Mihai Erős
  • 1,129
  • 1
  • 11
  • 18
0

It would be better if you can create custom cells. rather with your same code you just below line in CellForRowAtIndex method.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let identifier = "CATEGORY_CELL"
            let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
            let selectedIndexPaths = tableView.indexPathsForSelectedRows
            let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)

            cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none
            cell.textLabel?.text = list[indexPath.row].name
            cell.imageView?.image = list[indexPath.row].image // make sure you are saving images in struct.
            return cell
        }

initalize as

init(name:String, image:UIImage) {
            self.name = name
            self.image = image
        }

change func as

static func getCategories() -> [QCategoryy] {
        let list:[QCategoryy] = [QCategoryy(name: "name", image: UIImage(named: "imageName")!)]
        return list
    }

extension code:

extension QCategoryy: ExpressibleByStringLiteral {
     init(stringLiteral value: String) {
            self.name = value
            self.image = UIImage()
        }
        init(unicodeScalarLiteral value: String) {
            self.init(name: value, image: UIImage())
        }
        init(extendedGraphemeClusterLiteral value: String) {
            self.init(name: value, image: UIImage())
        }
    }
Rishi Chaurasia
  • 520
  • 4
  • 18
  • i have different images one for each name in the list –  Nov 07 '17 at 11:05
  • @Tvenden updated my code for your answer. :). Hope This will work – Rishi Chaurasia Nov 07 '17 at 11:07
  • thank you i think this can work, but last question, where i have to add the list of the images? –  Nov 07 '17 at 11:11
  • at the time of initialization. init(name:String, image:UIImage) { self.name = name self.image = image } – Rishi Chaurasia Nov 07 '17 at 11:18
  • because as you see in my code i added init(image:UIImage) { self.image = image } in the struct but i get the error: "Declaration in only valid at file scope" –  Nov 07 '17 at 11:19
  • yes but i take in the extension this error: Argument labels '(name:)' do not match any available overloads –  Nov 07 '17 at 12:32