Mahmoud Qatamsh
Mahmoud Qatamsh

Reputation: 33

I want to put three arrays in one array on swift 4 (table view)

i want to put all the arrays in one array and if i can to put changing image code in an array

let priceData = ["$7.99", "$4.59", "$2.29", "$1.19", "$2.29" , "$1.19"]
    let appNAme = ["Minecraft: Pocket Edition","Enlight", "Geometry Dash", "Plague Inc.", "R.B.I. Baseball 15", "Heads Up!"]
    let reviewsNumberData = ["(167)", "(71)", "(975)", "(629)", "(12)", "(5)"]


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

    let cell = myTableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCellTableViewCell

                // Setting Revies Number Label

    cell.reviewsNumberLabel.text = reviewsNumberData[indexPath.row]

                // Setting In-App Purchases label

        // Hide In-App Purchases in Row 1 ---
    if(indexPath.row == 1) {
        cell.inappLabel.isHidden = true
    }
        // Hide In-App Purchases in Row 2 ---
    if(indexPath.row == 2) {
        cell.inappLabel.isHidden = true
    }

        // Hide In-App Purchases in Row 4 ---
        if(indexPath.row == 4) {
            cell.inappLabel.isHidden = true
    }
                // Setting Counter for Apps

    cell.appCount.text = "\(indexPath.row + 1)"

                // Setting Type of App

    if(indexPath.row == 1){
        cell.appTypeLabel.text = "Photo & Video"
    }

                // Setting App Name

    cell.appNameLabel.text = appNAme[indexPath.row]

                // Setting Price Button

    cell.priceButton.titleLabel?.text = "ddd"
    cell.priceButton.setTitle(priceData[indexPath.row], for: .normal)

                 // Setting Image Change

    // Enlight
    if(indexPath.row == 1){
        cell.appImage.image = UIImage(named: "Enlight")
    }

    // Geometry Dash
    if(indexPath.row == 2){
        cell.appImage.image = UIImage(named: "GeometryDash")
    }

    // Plague Inc
    if(indexPath.row == 3){
        cell.appImage.image = UIImage(named: "plague")
    }
    // Rbi Baseball
    if(indexPath.row == 4){
        cell.appImage.image = UIImage(named: "Baseball")
    }
    // Heads Up !
    if(indexPath.row == 5){
        cell.appImage.image = UIImage(named: "HeadsUp")
    }


    return cell

Upvotes: 2

Views: 109

Answers (2)

RajeshKumar R
RajeshKumar R

Reputation: 15758

Create a struct with the properties

struct Item {
    var price: String
    var name: String
    var reviews: String
    var image: UIImage
    var inappLabelShow = false
}

And create an array of Item

var items = [Item]()

Use this array in tableview data source methods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = myTableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCellTableViewCell

    cell.reviewsNumberLabel.text = items[indexPath.row].reviews
    cell.appImage.image = items[indexPath.row].image
    cell.appNameLabel.text = items[indexPath.row].name
    cell.priceButton.setTitle(items[indexPath.row].price, for: .normal)
    cell.inappLabel.isHidden = items[indexPath.row].inappLabelShow
    return cell
}

You can create the struct object by enumerating the array objects

let item1 = Item(price: "$7.99", name: "Minecraft: Pocket Edition", reviews: "(167)", image: nil)
items.append(item1)
let item2 = Item(price: "$4.59", name: "Enlight", reviews: "(71)", image: UIImage(named: "Enlight"), inappLabelShow: true)
items.append(item2)

Upvotes: 5

Chris
Chris

Reputation: 4391

Instead of lots of arrays, a more readable and reliable way to achieve this is by keeping your data in a struct.

struct appItem {
    var name: String
    var price: Double
    var imageName: String // Store as string 
    var reviews: Int
    var inAppPurchases: Bool
    // These properties could all be strings if you prefer
}

You can make an array of these custom items:

var apps = [AppItem]()
apps.append(name: "My App", price: 7.00, imageName: "appImage1", reviews: 23, inAppPurchases: true)
// Add as many as you like

Then access the data in the tableview delegate method:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = myTableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCellTableViewCell else { return }
    cell.nameLabel.text = apps[indexPath.row].name
    cell.imageView.image = UIImage(named: apps[indexPath.row].imageName)
    // Repeat for other properties
    return cell
}

Upvotes: 0

Related Questions