Ios 使用自定义标签创建新行/单元格时遇到问题

Ios 使用自定义标签创建新行/单元格时遇到问题,ios,swift,uitableview,Ios,Swift,Uitableview,使用Swift,我的自定义单元格有4个标签,我想在将新行/单元格添加到tableview时编辑这些标签 在我的应用程序顶部的工具栏上,我有一个add按钮,用来将新的单元格/行添加到表格视图中,但只有第一个按钮起作用,当我添加第二个单元格/行时,它会改变并变小 我知道代码显然是错误的,但我似乎无法修复它,因为它使用了一个名为objects的数组,但我似乎无法向要在表视图中使用的对象添加多个字符串 我在项目中使用标准的主细节模板,但在根据自己的喜好将代码更改为自定义时,我遇到了这些问题 overri

使用Swift,我的自定义单元格有4个标签,我想在将新行/单元格添加到tableview时编辑这些标签

在我的应用程序顶部的工具栏上,我有一个add按钮,用来将新的单元格/行添加到表格视图中,但只有第一个按钮起作用,当我添加第二个单元格/行时,它会改变并变小

我知道代码显然是错误的,但我似乎无法修复它,因为它使用了一个名为objects的数组,但我似乎无法向要在表视图中使用的对象添加多个字符串

我在项目中使用标准的主细节模板,但在根据自己的喜好将代码更改为自定义时,我遇到了这些问题

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(u:heightForRowAt:)->CGFloat


请尝试返回88.0f并查看差异

根据我们之前的对话,尝试以下内容:

@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
}

对不起,我不太明白你的问题。您是说在tableViewCell中显示所有标签时出现问题吗?尝试通过将此方法添加到视图控制器来增加tableViewCell的高度:
func tableView(\uTableView:UITableView,heightForRowAt indexPath:indexPath)->CGFloat{return 200}
为了防止创建自定义字符串对象,还可以简单地使用多维数组,这意味着您将一个包含四个字符串(每行一个)的数组放入对象数组:
让objects=[[“A1”、“A2”、“A3”、“A4”]、[“B1”、“B2”、“B3”、“B4”]、[“C1”、“C2”、“C3”、“C4”]
如果使用多维数组,您只需像这样分配字符串值:
让currentSubarray=objects[indexPath.row]
cell.firstLabel.text=currentSubarray[0]
cell.firstLabel.text=currentSubarray[1]
cell.firstLabel.text=currentSubarray[2]
cell.firstLabel.text=currentSubarray[3]
如果你喜欢我的建议,如果你支持我的意见,我将不胜感激。)如果要插入新对象,基本上只需将新的子数组附加到对象数组属性,然后重新加载tableView即可。请注意,您应该将其定义为“var”,以便使其可变
self.objects.append([“X1”、“X2”、“X3”、“X4”])
self.myTableView.reloadData()
否,如果要操作表视图的数据源(添加更多单元格),请创建一次多维对象数组并附加更多子数组。基本上,您也可以这样访问相应的子数组:
self.firstLabel.text=objects[indexPath.row][0]
self.secondLabel.text=objects[indexPath.row][1]
self.thirdLabel.text=objects[indexPath.row][2]
self.fourthLabel.text=objects[indexPath.row][3]
但如果这样做,代码看起来更冗余