使用Swift,我的自定义单元格有4个标签,我想在将新行/单元格添加到tableview时编辑。
在我的应用程序栏中,我有一个添加按钮,它应该将新的单元格/行添加到表视图中,但只有第一个可以工作,当我添加第二个单元格/行时,它会更改并变小
我知道代码显然是错误的,但我似乎无法修复它使用一个名为objects的数组,但我似乎无法将多个字符串添加到要用于的对象中表格视图。
我使用标准的Master Detail模板进行项目,但是根据我的喜好将代码更改为自定义,我遇到了这些问题。
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
//let object = objects[indexPath.row] as! String // NSDate
//cell.textLabel!.text = object.description
cell.firstLabel.text = "name"
cell.secondLabel.text = "bought"
cell.thirdLabel.text = "18"
cell.fourthLabel.text = "30"
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
objects.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
@objc
func insertNewObject(_ sender: Any) {
// Adding cell with new added info
// can not be an array of strings, has to be an array of an array of strings, doesn't it?
// "tester" is was there from the project template
let test: String = "tester"
objects.insert(test, at: 0) // NSDate()
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = objects[indexPath.row] as! NSDate
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
@IBOutlet var myTableView: UITableView!
var detailViewController: DetailViewController? = nil
var objects = [Any]()
override func viewDidLoad() {
super.viewDidLoad()
// Custom Cell
self.myTableView.dataSource = self
self.myTableView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
if let split = splitViewController {
let controllers = split.viewControllers
detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
}
答案 0 :(得分:2)
我认为你应该尝试实现方法tableView(_:heightForRowAt :) - > CGFloat的
请尝试返回88.0f并查看差异
答案 1 :(得分:1)
根据我们之前的对话尝试这样的事情:
@IBOutlet private weak var tableView: UITableView!
private var objects = [["A1", "A2", "A3", "A4"],["B1", "B2", "B3", "B4"],["C1", "C2", "C3", "C4"],["D1", "D2", "D3", "D4"]]
private func insertNewObject() {
self.objects.append(["NewString1", "NewString2", "NewString3", "NewString4"])
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.objects.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200 // add the height you like
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.firstLabel.text = objects[indexPath.row][0]
cell.secondLabel.text = objects[indexPath.row][1]
cell.thirdLabel.text = objects[indexPath.row][2]
cell.fourthLabel.text = objects[indexPath.row][3]
return cell
}