【发布时间】:2017-10-16 07:53:15
【问题描述】:
所以我想通过按钮“移动到新的 tableview”(检查图片)将我的 tableview 从“ViewController”转移到我的“SecondViewController”。 在我的表格视图中,我希望使用 pelaajat.append 添加所有单元格
模拟器和故事板图片如下。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var tableView: UITableView!
var pelaajat = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView(frame: CGRect.zero)
}
@IBAction func movetoNewTableview(_ sender: Any) {
}
@IBAction func addName(_ sender: Any) {
lisaaPelaajanNimi()
}
func lisaaPelaajanNimi(){
pelaajat.append(textField.text!)
let indexPath = IndexPath(row: pelaajat.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
textField.text = ""
view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pelaajat.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = pelaajat[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
pelaajat.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
}
【问题讨论】:
-
你想要的是将单元格(数据)移动到另一个 UIViewController 中的另一个 tableview 吗?
-
是的,并且在 tableview 内的其他 UIViewController 上也
-
传递数据并在tableView的第二个视图控制器中显示...为什么要移动单元格?试图实现这一点,以便我可以帮助你
-
您不移动视图,您传递的数据可以由 UITableView 表示。
标签: ios iphone swift uitableview tableview