我正在尝试添加3个项目以及一个tableview单元格,现在它仅将第一个文本字段中的项目打印一次(如图片所示)三遍,我需要它先打印第一个项目,然后再打印第二个项目,然后再打印第三个项目。我对自己做错的事情感到困惑,当我也单击添加按钮时,应用程序完全崩溃。
import UIKit
import SafariServices
class ViewController: UIViewController, UIAdaptivePresentationControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var OrderNumberLabel: RoundedLabel2!
@IBOutlet weak var CostLabel: RoundedLabel2!
@IBOutlet weak var ProfitLabel: RoundedLabel2!
@IBOutlet weak var TotalLabel: RoundedLabel2!
@IBOutlet weak var itemTextField: UITextField!
@IBOutlet weak var priceTextField: UITextField!
@IBOutlet weak var saleTextField: UITextField!
var items: [ItemRow] = []
struct ItemRow
{
var first: String
var second: String
var third: String
}
override func viewDidLoad() {
super.viewDidLoad()
// tableView.tableFooterView = UIView(frame: CGRect.zero)
}
@IBAction func addButtonTapped(_ sender: Any) {
insertNewItems()
}
func insertNewItems() {
let newVideoRow = ItemRow(first: itemTextField.text!, second: priceTextField.text!, third: saleTextField.text!)
items.append(newVideoRow)
let indexPath = IndexPath(row: items.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
itemTextField.text = ""
priceTextField.text = ""
saleTextField.text = ""
view.endEditing(true)
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let ItemTitle = items[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell") as! ItemCell
cell.itemLabel.text = ItemTitle.first
cell.priceLabel.text = ItemTitle.second
cell.saleLabel.text = ItemTitle.third
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
items.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
答案 0 :(得分:0)
所以我的理解是,您期望以下内容:-
让我们以第一种情况为例,即您要添加三行。为此,您正在向videos
数组中正确添加三行,并且datasource
扩展名是正确的(尽管具有三个具有相同值的文本字段的用处尚不明确)。您的错误在于表更新-您正在告诉它在行中添加了额外的一行:
let indexPath = IndexPath(row: videos.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
如果要添加三行,则需要告诉它添加了三行,即:-
let indexPath1 = IndexPath(row: videos.count - 3, section: 0)
let indexPath2 = IndexPath(row: videos.count - 2, section: 0)
let indexPath3 = IndexPath(row: videos.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath1, indexPath2, indexPath3], with: .automatic)
tableView.endUpdates()
另一方面,如果您只希望包含所有三行的一行添加,那么您的行:-
videos.append(addVideoTextField.text!)
videos.append(addVideoTextField2.text!)
videos.append(addVideoTextField3.text!)
不正确。相反,您将需要使用一种结构来包含条目,并创建一个新实例并将其添加到videos
中。例如。构造一个结构:-
struct VideoRow
{
var first: String
var second: String
var third: String
}
将此作为数据源,即,将声明更改为:-
var videos: [VideoRow] = []
在插入函数中添加添加:-
let newVideoRow = VideoRow(first: addVideoTextField.text!, second: addVideoTextField2.text!, third: addVideoTextField3.text!)
videos.append(newVideoRow)
您的cellForRowAt
然后变为:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let videoTitle = videos[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell") as! VideoCell
cell.videoTitle.text = videoTitle.first
cell.videoTitle2.text = videoTitle.second
cell.videoTitle3.text = videoTitle.third
return cell
}