In first VC I have an array with images (which I picked - array.append) which goes over prepareforsegue to another VC with tableview, and tableview reads that array and everything works, however when going back from tableview to first VC to pick another set of images (array.append) tableview is populating cells with set of previous picked images, because array have previous picked images. How could I make that tableview remember only the last added images.
ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var image1: UIImageView!
@IBOutlet weak var image2: UIImageView!
@IBOutlet weak var image3: UIImageView!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
var array = [UIImage]()
var array2 = [String]()
var number = 0
@IBAction func gamb(sender: UIButton) {
}
@IBAction func gamb2(sender: UIButton) {
array.append(self.image2.image!)
array2.append(self.label2.text!)
}
@IBAction func gamb3(sender: AnyObject) {
array.append(self.image1.image!)
array2.append(self.label1.text!)
}
@IBAction func gamb4(sender: UIButton) {
array.append(self.image3.image!)
array2.append(self.label3.text!)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destVC = segue.destinationViewController as! TableViewController
let priljepak = self.array
destVC.array = priljepak
let priljepak2 = self.array2
destVC.array2 = priljepak2
}
}
TableViewController
import UIKit
class TableViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var array = [UIImage]()
var array2 = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.imageView?.image = array[indexPath.row]
cell.textLabel!.text = array2[indexPath.row]
return cell
}
}
arrayandarray2is very confusing. It would be much better to use names likeimageArrayandlabelArrayso that you know what's inside them. :D