How extract from UITableView data (UIImage and String) to another ViewController

Eduardo Navarrete

I want to pass the storage data contained in a TableView and pass it to another ViewController. My customCell has an UIImage and a String. When the user press the cell I want to show a "detail view controller" with the UIImage and a label containing the info of the cell selected.

Here is my code:

import UIKit


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var dataTableView: UITableView!

var myList = [dataList]()

var textToBeSent: String = ""

var selectedImage: UIImage?
var selectedLabel: String?

//Load Items To List

func loaditems(){
    let item1 = dataList(photoList: UIImage.self(), itemDescription: "Descripcion Aqui")
    let item2 = dataList(photoList: UIImage.self(), itemDescription: "Aqui tmb")

    myList += [item1,item2]
}

//var list = ["Documento 1", "Documento 2", "Documento 3"]

override func viewDidLoad() {
    super.viewDidLoad()

    if let savedData = loadSavedItems(){
        myList += savedData
    } else {

    loaditems()
    }


    //dataTableView.register(UITableViewCell.self, forCellReuseIdentifier: "reusablecell")


    // Do any additional setup after loading the view, typically from a nib.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return myList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "prototype", for: indexPath) as! PrototypeCell

    let itemsinCell = myList[indexPath.row]

    cell.imageItem.image = itemsinCell.photoList
    cell.itemDescription.text = String(itemsinCell.itemDescription)

    return cell
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete{
        myList.remove(at: indexPath.row)
        dataTableView.reloadData()
    }

    saveToSavedData()
}

Here is the func where I want to pass the data of a certain cell. The data is from a Swift file stored in a "DataList" using aDecoder NSCoder.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    print("Row \(indexPath.row) selected")
    selectedImage! = myList[indexPath.row].photoList
    selectedLabel! = myList[indexPath.row].itemdescription
    performSegue(withIdentifier: "selectedRowSegue", sender: myList[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "selectedRowSegue"){
        let chosenRowViewController = segue.destination as! chosenRowViewController
        chosenRowViewController.image3 = selectedImage?.photoList
        chosenRowViewController.label3 = selectedLabel?.itemDescription
    }
}

Unwind a segue in order to fill up the cells with data from previous ViewController:

//Unwinde Segue
@IBAction func unWindlToList(sender: UIStoryboardSegue){
    if let sourceViewController = sender.source as? ProcessViewController, let item = sourceViewController.item{
        let newIndexPath = IndexPath(row: myList.count, section: 0)
        myList.append(item)
        dataTableView.insertRows(at: [newIndexPath], with: .automatic)
    }
    saveToSavedData()
}
//Archive Data
func saveToSavedData(){
    NSKeyedArchiver.archiveRootObject(myList, toFile: (dataList.fileFolder?.path)!)
}
//Unarchive Data
func loadSavedItems() -> [dataList]?{
    return NSKeyedUnarchiver.unarchiveObject(withFile: (dataList.fileFolder?.path)!) as? [dataList]
}

}

class PrototypeCell: UITableViewCell {

    @IBOutlet weak var itemDescription: UILabel!

    @IBOutlet weak var imageItem: UIImageView!

}
Lance Samaria

There are several things that stand out.

1- var myList = [dataList]() dataList is a Class, classes should be capitalized. It should be var myList = [DataList]()

2- You have this as a class property but it's not used anywhere in the code you posted so why did you add it and what is it's purpose? var textToBeSent: String = ""

3- You have these 2 class property variables

var selectedImage: UIImage?
var selectedLabel: String?

to hold the data from [myList] but you really don't need them because you can just access the data from [myList] using dot notation inside prepareForSegue (read the commented out code in prepareForSegue).

4- In prepareForSegue you have let chosenRowViewController = segue.destination as! chosenRowViewController. ChosenRowViewController is a class and it should be capitalized like so:

let chosenRowViewController = segue.destination as! ChosenRowViewController // it's capitalized after the as!

Here's the code a little cleaned up.

@IBOutlet weak var dataTableView: UITableView!

var myList = [DataList]()

func loaditems(){
    let item1 = dataList(photoList: UIImage.self(), itemDescription: "Descripcion Aqui")
    let item2 = dataList(photoList: UIImage.self(), itemDescription: "Aqui tmb")

    myList.append(item1)
    myList.append(item2)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // everything you already have inside here...
}

// your tableView datasource methods...

5- Since your using prepareForSegue you don’t need didSelectRowAt indexPath

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "selectedRowSegue"){

        // get the indexPath for the selectedRow
        let indexPath = tableView.indexPathForSelectedRow

        let chosenRowViewController = segue.destination as! ChosenRowViewController
        chosenRowViewController.image3 = myList[indexPath!.row].photoList // use dot notation to access the photoList property
        chosenRowViewController.label3 = myList[indexPath!.row].itemDescription  // use dot notation to access the itemDescription property
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extract UIImage from NSAttributed String

From Dev

How to fill TableView with data from another ViewController

From Dev

Updating a UITableView in Swift from another ViewController

From Dev

Updating a UITableView in Swift from another ViewController

From Dev

How to create CGImageRef from NSData string data (NOT UIImage)

From Dev

How to use extract data from this string

From Dev

How to extract data in Quotes from a string in Excel

From Dev

How to extract specific data from a string in JavaScript?

From Dev

How to extract colours from UIImage using Swift

From Dev

How do I extract a string from another string php

From Dev

Extract data from string

From Dev

Extract data from string

From Dev

Passing string from UISearchBar to another viewController

From Dev

How to extract number values from an array and then combine it with another string?

From Dev

Reload data for UIPickerView from another ViewController

From Dev

Initialization Data after it receiving from another ViewController

From Dev

How to pass an UIImage from UIImagePickerController to another UIViewController?

From Dev

Passing Core Data attributes from viewController to another viewController

From Dev

Swift: Passing data from a tableView in a ViewController to another ViewController

From Dev

How to extract parsed data from once cell to another

From Dev

Trying to Display a UIImage from JPEG Binary Data (How do I Convert a String to Data)

From Dev

Extract String from another String in Javascript

From Dev

How to change textView in separate ViewController from button in another ViewController?

From Dev

How to pass NSURL object from one viewcontroller to another viewcontroller

From Dev

How can I extract data from one pixel in an image and assign this data to another pixel in another image?

From Dev

How to segue from one viewcontroller to another

From Dev

How to call function from another ViewController

From Dev

How to call another ViewController from the code?

From Dev

How to call function from another ViewController

Related Related

  1. 1

    Extract UIImage from NSAttributed String

  2. 2

    How to fill TableView with data from another ViewController

  3. 3

    Updating a UITableView in Swift from another ViewController

  4. 4

    Updating a UITableView in Swift from another ViewController

  5. 5

    How to create CGImageRef from NSData string data (NOT UIImage)

  6. 6

    How to use extract data from this string

  7. 7

    How to extract data in Quotes from a string in Excel

  8. 8

    How to extract specific data from a string in JavaScript?

  9. 9

    How to extract colours from UIImage using Swift

  10. 10

    How do I extract a string from another string php

  11. 11

    Extract data from string

  12. 12

    Extract data from string

  13. 13

    Passing string from UISearchBar to another viewController

  14. 14

    How to extract number values from an array and then combine it with another string?

  15. 15

    Reload data for UIPickerView from another ViewController

  16. 16

    Initialization Data after it receiving from another ViewController

  17. 17

    How to pass an UIImage from UIImagePickerController to another UIViewController?

  18. 18

    Passing Core Data attributes from viewController to another viewController

  19. 19

    Swift: Passing data from a tableView in a ViewController to another ViewController

  20. 20

    How to extract parsed data from once cell to another

  21. 21

    Trying to Display a UIImage from JPEG Binary Data (How do I Convert a String to Data)

  22. 22

    Extract String from another String in Javascript

  23. 23

    How to change textView in separate ViewController from button in another ViewController?

  24. 24

    How to pass NSURL object from one viewcontroller to another viewcontroller

  25. 25

    How can I extract data from one pixel in an image and assign this data to another pixel in another image?

  26. 26

    How to segue from one viewcontroller to another

  27. 27

    How to call function from another ViewController

  28. 28

    How to call another ViewController from the code?

  29. 29

    How to call function from another ViewController

HotTag

Archive