Coding while Loading
Coding while Loading

Reputation: 413

swift table view cells does not let me use indexPath.row

i am trying to populate data into cells inside of a table view. I created a chosenPlanData var which is initialized to an object with the data inside of it.. The object has properties such as "name" and "event location". An issue occurs when inside of 'cellForRowAt'. It does not let me add [indexPath.row] to the cell i am creating, which in turn does not populate the cells correctly.

For instance - i removed indexPath.row from the first cell.nameLbl.text call - and in turn every single name label in the table view was the same. here is piece of the code

 var chosenPlanData = ChosenPlan()   

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


        guard let cell = tableView.dequeueReusableCell(withIdentifier: "individualPlansCell") as? PlanitsHomeViewCell else { return UITableViewCell() }


          cell.nameLbl.text = chosenPlanData.nameOfEvent[indexPath.row] // error Cannot assign value of type 'Character' to type 'String?'

        cell.dateAndTimeLbl.text = chosenPlanData.eventStartsAt[indexPath.row] as? String // error 'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion

      cell.nameLbl.text = chosenPlanData.nameOfEvent // This works - but every single cell has the same nameLbl though


        return cell

    }


   // Here is the call back where i initialize the value for chosenPlanData

        let EventbriteTVC = segue.destination as! EventbriteTableView

        EventbriteTVC.callbackChosePlan = { result in


            DispatchQueue.main.async {
                self.individualPlanitsTableView.reloadData()
            }

            self.chosenPlanData = result



        }

import Foundation import UIKit

class ChosenPlan {

var nameOfEvent : String = ""
var eventStartsAt : String = ""
var eventLocationIs : String = ""
var eventURL : String = ""
var imageForPlan : String?


convenience init( eventName: String, eventTime: String, eventLocation: String, eventImage: String){

    self.init()

    self.nameOfEvent = eventName
    self.eventStartsAt = eventTime
    self.eventLocationIs = eventLocation
    //self.eventURL = eventLink
    self.imageForPlan = eventImage

    //eventLink: String,
}

}

Upvotes: 0

Views: 1343

Answers (4)

Manish Mahajan
Manish Mahajan

Reputation: 2082

if

var chosenPlanData = [ChosenPlan]()

Try this:-

cell.nameLbl.text = "\(chosenPlanData[indexPath.row].nameOfEvent)"

or

cell.nameLbl.text = "\(chosenPlanData[indexPath.row].nameOfEvent ?? "")"

Upvotes: 0

dahiya_boy
dahiya_boy

Reputation: 9503

extending my comment

  1. var chosenPlanData = ChosenPlan()

chosenPlanData is object of ChosenPlan

  1. Now in cellForRow you writing chosenPlanData.nameOfEvent[indexPath.row] but nameOfEvent is String as per your ChosenPlan that you mentioned in question.

For more info,

chosenPlanData.nameOfEvent[indexPath.row] this line represents you using the n th (indexPath.row) object of nameOfEvent which is object of chosenPlanData

Hope now will be more cleared.

Solution

  1. var chosenPlanData = [ChosenPlan]() <- create array

  2. In cellForRow chosenPlanData[indexPath.row].nameOfEvent that means you'r using nameOfEvent of nth object of chosenPlanData.

Upvotes: 2

Lumialxk
Lumialxk

Reputation: 6369

Type handling is very important in Swift. Try this.

cell.nameLbl.text = String(chosenPlanData.nameOfEvent[indexPath.row])
let index = chosenPlanData.eventStartsAt.index(chosenPlanData.eventStartsAt.startIndex, offsetBy: 3)
cell.dateAndTimeLbl.text = String(chosenPlanData.eventStartsAt[index])

Upvotes: 0

Paulw11
Paulw11

Reputation: 114783

Your chosenPlanData variable is a single instance of ChosenPlan - You cannot subscript a single instance.

It needs to be an array of ChosenPlan:

var chosenPlanData =  [ChosenPlan]()

Then you can index into this array:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "individualPlansCell") as? PlanitsHomeViewCell else { return UITableViewCell() }

          cell.nameLbl.text = chosenPlanData[indexPath.row].nameOfEvent 
          cell.dateAndTimeLbl.text = chosenPlanData[indexPath.row].eventStartsAt

Upvotes: 3

Related Questions