简体   繁体   中英

add a row to the tableView from another viewController

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).

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 .

my problem: 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!

在此处输入图片说明

popup VC

@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.add(name: nameP.text!, link: linkP.text!)
}

HomeScreen VC

class TEstVC: UIViewController, UITableViewDelegate, UITableViewDataSource {



@IBOutlet weak var tableView: UITableView!

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



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

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

override func viewWillAppear(_ animated: Bool) {


}

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 = names[indexPath.row]
    cell.cell4Link.text = links[indexPath.row]

    return cell
}

Move your array declaration to global and call reloadData in your tableView like this:

//Popup

@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.add(name: nameP.text!, link: linkP.text!)
    self.tableView.reloadData()
}

Your VC

var names = [String]()
var links = [String]()
class TEstVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableView: UITableView!



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

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

override func viewWillAppear(_ animated: Bool) {


}

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 = names[indexPath.row]
    cell.cell4Link.text = links[indexPath.row]

    return cell
}

Well, it looks like you're going to a completely new instance of TestVC every single time. So, it's not overwriting the old line. It's putting a new line in a new instance of TestVC. I infer this because you're calling add(name:link:) in prepareForSegue , which gets called by the storyboard when a new viewController is being put on screen with a segue.

Also, your add(name:link:) method should call tableView.reloadData() if you want it to update the screen.

很简单,在将新项添加到数据源(名称)后,您需要重新加载表视图。

self.tableView.reloadData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM