WokerHead
WokerHead

Reputation: 967

Having trouble in creating new row/ cell with custom labels

Using Swift, My custom cell has 4 labels which I would like to edit when adding the new row/cell to the tableview.

On top of my app in the bar I have an add button which is supposed to add the new cell/row to the table view but only the first one works and when I add a second cell/row it changes and becomes smaller.

I know the code is wrong obviously but I can't seem to fix it as its using an array called objects but I can't seem to be able to add multiple strings to the object to be used in the table view.

I'm using the standard Master Detail template for the project but in changing the code to be custom to my liking I ran into these problems.

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

pic1

Upvotes: 1

Views: 89

Answers (2)

Objective D
Objective D

Reputation: 809

according to our previous conversation try something like this:

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

Upvotes: 1

Kien Tran
Kien Tran

Reputation: 101

I think you should try to implement method tableView(_:heightForRowAt:) -> CGFloat

Please try to return 88.0f and see the difference

Upvotes: 2

Related Questions