ios - 如何传递数据并在保存旧行的情况下向 TableView 添加一行

标签 ios tableview row addition

到目前为止我做了什么:

我创建了一个带有空行的 tableView Controller ,并在另一个 ViewController 上创建了一个弹出窗口,弹出窗口的目的是将两个数据(名称 - 链接)添加到一个 ROW 上的 tableView(传递文本字段)。

我的问题:

当我点击弹出窗口中的保存按钮时,数据被很好地传递并创建了一个新行(名称 - 链接)..

但是当我尝试添加更多原料时,它总是比旧原料好, 所以无论我输入多少数据,结果总是 1 行!

我想要什么:

当我添加一个新的原始(名称 - 链接)点击保存时,我希望存储数据,当我添加其他数据时,创建另一行..

她是我在 TableView 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
}

弹出 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

::::::更新。::::

enter image description here

最佳答案

您的类“popup_main”在关闭时引用了它的目的地,并且您将“passname”和“passlink”的值设置为输入到文本字段中的字符串。

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

请注意,您的“tableView”将调用:

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

对于表格中的每一行,由:

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

如果你仔细检查你的代码,你会注意到这意味着每次调用它都会使用值“passname”和“passlink”(并且你每次关闭弹出窗口时都会覆盖它们!) ,您实际上并没有向“名称”和“链接”数组添加新名称和链接。

您应该完全删除“密码名”和“密码链接”属性,然后在“TEstVC”类中添加一个新函数,例如:

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

并从您的弹出窗口中调用它:

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

还将您的 cellForRowAt 实现更新为:

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
}

我建议您阅读有关 UITableView 的文档,因为我认为您可能误解了它们的功能!文档可以在这里找到:

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

祝你好运!

编辑(23/04/2018):

由于您的“viewWillAppear”方法,您可能仍然遇到麻烦。尝试将其更改为这样的内容:

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

这将在 View 出现时用动画更新第一部分(注意:您目前只在 tableView 中使用一个部分“IndexSet(integer: 0)”)

关于ios - 如何传递数据并在保存旧行的情况下向 TableView 添加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49968501/

相关文章:

ios - View Controller 之间的一类实例化快速共享

ios - 具有不同大小单元格的 UICollectionView

ios - Xcode 不遵守设备方向

iphone - 如何在分离的 UITableViewCell 和另一个 View 之间执行 segue

swift - 使用 Swift 在 Xcode 中填充 TableView

ios - XCode 4 "add build setting condition"未启用

ios - 在 IOS 中从 iPhone 中删除 Assets (图片或视频)

javascript - JQgrid设置行高

php - MYSQL 获取行到不同的表

google-apps-script - Google 表格脚本 - 将行从一个选项卡 move 到另一个选项卡