0

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
}
}
5
  • Using the names array and array2 is very confusing. It would be much better to use names like imageArray and labelArray so that you know what's inside them. :D Commented Nov 24, 2015 at 13:43
  • yea i know, it's a sort of challenge for me to remember which array is for what :) Commented Nov 24, 2015 at 17:20
  • haha. Even better you could use a struct called "ImageThing" which has an image and some text. Then you would only have one array of ImageThings. Commented Nov 24, 2015 at 17:37
  • Google it. Or download the intro to swift book from Apple. Commented Nov 24, 2015 at 22:40
  • i know how to type structs, what i'm interested is array with multi items. Anyway Have problem with arrays in this project with structs (struct classes not copy pasted, only vc's) :)), stackoverflow.com/questions/33746340/…, maybe you can help because no one is :), thank you Commented Nov 25, 2015 at 2:33

2 Answers 2

2

In the first ViewController, override viewWillAppear. Call super and empty the arrays.

override func viewWillAppear(animated:bool) {
    super.viewWillAppear(animated)
    array = [UIImage]()
    array2 = [String]()
}

This'll cause any selection to be removed when returning to the first VC.

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

Comments

1

Insert the following override into your ViewController:

override func viewWillAppear(animated: Bool){
    super.viewWillAppear(animated)

    array.removeAll()
    array2.removeAll()

}

This will clean the arrays in your first view controller, when TableViewController is dismissed.

Comments

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.