Ahmed
Ahmed

Reputation: 61

how to pass a data and add a row to the tableview with saving the old rows

what I did so far :

I create a tableView controller with empty rows , and a popup window on another ViewController ,, the purpose of the popup is to add two data(name - link) to the tableView on one ROW (passing Textfields).

my problem :

when I click to the save button in the popup window , the data are passed very fine and create a new row with (name - link)..

but when I tray to add more raws ,it's always wright over the old one , so no matter how much data I input , the result always be 1 row!

what I want :

when I add a new raw (name - link) click save , I want the data to be stored ,, when I add other data , create another row ..

hers is what I did on table view controller :

import UIKit

class TEstVC: UIViewController, UITableViewDelegate, UITableViewDataSource {



@IBOutlet weak var tableView: UITableView!

var names = [String]()
var links = [String]()

var passname = ""
var passlink = ""

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.rowHeight = 100.0
    tableView.dataSource = self
    tableView.delegate = self
}


override func viewWillAppear(_ animated: Bool) {
  //  tableView.insertRows(at: [IndexPath(row: names.count-1, section: 0)], with: .automatic)
    names.append("")
    tableView.beginUpdates()
    tableView.insertRows(at: [IndexPath(row: names.count-1, section: 0)], with: .automatic)
    tableView.endUpdates()
    tableView.reloadData()
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return names.count //or links.count
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
    cell.cell4Name.text = passname
    cell.cell4Link.text = passlink

    return cell
}

popup view controller

import UIKit

class popup_main: UIViewController {


@IBOutlet weak var nameP: UITextField!

@IBOutlet weak var linkP: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()


}





override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let SEC: TEstVC  = segue.destination as! TEstVC
    SEC.passname = nameP.text!
    SEC.passlink = linkP.text!

}

enter image description here

::::: UPDATE. ::::

enter image description here

Upvotes: 0

Views: 53

Answers (1)

Zyth
Zyth

Reputation: 11

Your class 'popup_main' has a reference to it's destination when it's dismissed and you're setting the values of 'passname' and 'passlink' to the String's entered into the text fields.

let SEC: TEstVC  = segue.destination as! TEstVC
SEC.passname = nameP.text!
SEC.passlink = linkP.text!

Note that your 'tableView' will call:

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

For EACH row in your table, provided by:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return names.count //or links.count
}

If you double check your code, you'll notice that this means that each time this is called it will use the values 'passname' and 'passlink' (And you're overriding them each time you dismiss your popup!), you're not actually ever adding new names and links to your 'names' and 'links' arrays.

You should remove your 'passname' and 'passlink' properties entirely and then add a new function in your 'TEstVC' class such as:

func add(name: String, link: String) {
  names.append(name)
  links.append(link)
}

and call this from your popup:

let SEC: TEstVC  = segue.destination as! TEstVC
SEC.add(name: nameP.text!, link: linkP.text!)

Also update your implementation of cellForRowAt to:

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
    cell.cell4Name.text = names[indexPath.row]
    cell.cell4Link.text = links[indexPath.row]

    return cell
}

I'd recommend you read the documentation on UITableView's as I think you may have misinterpreted how they function! The documentation can be found here:

https://developer.apple.com/documentation/uikit/uitableview

Good luck!

Edit (23/04/2018):

You're probably still having trouble because of your 'viewWillAppear' method. Try changing it to something like this:

override func viewDidAppear(_ animated: Bool) {
    tableView.reloadSections(IndexSet(integer: 0), with: .automatic)
}

This will update the first section with an animation when the view appears (note: you're currently only using one section 'IndexSet(integer: 0)' in your tableView)

Upvotes: 1

Related Questions