0

I would like to populate so many rows in a tableview as I have entries defined. At my actual code I receive only the first item.

What I expect is to have all 3 countries populated.

my code looks like this:

class ViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// How many sections. Means numberOfSections * rows = view
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

// Defines the number of rows in a section table
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return 1
}

// Defines contet of each cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    //let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! myCell

    var countries =  ["Turkey","Croatia","USA"]

    var logoImageTUR = UIImage(named: "turkey.png")
    var logoimageCRO =  UIImage(named: "croatia.png")
    var logoimageUSA = UIImage(named: "USA.png")

    var countryLogos = [logoImageTUR, logoimageCRO, logoimageUSA]

    for var i = 0; i < 2; i++
    {
        cell.lbl_countryName.text = countries[i]
        cell.img_countryFlag.image = countryLogos[i]

        return cell
    }
}

}

Thank you

2 Answers 2

1

If you define your two list outside the cellForRowAtIndexPath and update the numberOfRowsInSection and the cellForRowAtIndexPath like in the following way:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

   var countries =  ["Turkey","Croatia","USA"]    
   var countryLogos = [UIImage(named: "turkey.png"), UIImage(named: "croatia.png"), UIImage(named: "USA.png") ]

   // Defines the number of rows in a section table
   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
      return countries.count
   }

   // Defines content of each cell
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! myCell

     // Populates with the indexPath.row according the position inside the list
     cell.lbl_countryName.text = countries[indexPath.row]
     cell.img_countryFlag.image = countryLogos[indexPath.row]

     return cell    
   }

   // Rest of code...

}

It should do what you want.

In the above code put only the code relevant to your question, you need the rest of your code to work, I mean the numberOfSectionsInTableView , etc. You need too implements the two protocols UITableViewDelegate and UITableViewDataSource or define a UITableViewController using Storyboards by the way.

I hope this help you.

Sign up to request clarification or add additional context in comments.

Comments

0
// Defines the number of rows in a section table
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return 1
}

Your data source reports that the table contains only a single row.

2 Comments

while increasing this value to 3 I receive just 3 times the the same entry. Means 3 rows turkey,turkey,turkey. And not 3 different entries like turkey, croatia, USA
Yes, look at your for var i = 0; i < 2; i++ loop. You set the name and flag of a cell to the first value in countries and countryLogos and then you set replace the name and flag of the same cell with the second value from those array, and again for the third. You never consider the row of the provided index path when populating the cell.

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.