使用 Swift,我的自定义单元格有 4 个标签,我想在将新行/单元格添加到 tableview 时编辑这些标签。
在栏中我的应用程序顶部,我有一个添加按钮,它应该将新的单元格/行添加到表格 View ,但只有第一个有效,当我添加第二个单元格/行时,它会发生变化并变得更小.
我知道代码显然是错误的,但我似乎无法修复它,因为它使用名为对象的数组,但我似乎无法将多个字符串添加到要在 TableView 中使用的对象。
我正在为项目使用标准的 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
}
}
最佳答案
我认为你应该尝试实现方法 tableView(_:heightForRowAt:) -> CGFloat
请尝试返回 88.0f 并查看差异
关于ios - 在使用自定义标签创建新行/单元格时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718550/