0

Specifically for a calculator app. I want to type in the textfield in the first row and have that change text in the other rows. Really, type in any row and change the value of the other rows.

I know I should be working with ints but I was going to worry about type converting later.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell

    //label and text_field come from CustomTableViewCell.swift
    cell.label.text = array[indexPath.row]
    cell.text_field.text = array_values[indexPath.row]

    return cell
}

This is inside my tableViewController class and thats the func that generates the rows from the arrays.

Im after something like when you type in a text field, hit a button, and it updates some label, but where theres no button and text fields update other text fields as the user types. Inside a table view.

Heres the rest of the code.

import UIKit

class TableViewController: UITableViewController {

@IBOutlet var table_view: UITableView!

var array = [String]()
var array_values = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    array = ["1", "2", "3", "4", "5"]
    array_values = ["", "", "", "", ""]

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
    cell.label.text = array[indexPath.row]
    cell.text_field.text = array_values[indexPath.row]

    return cell
}

}

import UIKit

class CustomTableViewCell: UITableViewCell {

@IBOutlet weak var text_field: UITextField!
@IBOutlet weak var label: UILabel!


override func awakeFromNib() {

    super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

  • just reload the table data after editing the field – Leo Dabus Jun 04 '16 at 04:11
  • Ok, how do I do that? How do I get the data from one text field, change the others and reload everything? Im pretty new to all this. – Kevin Corcoran Jun 05 '16 at 19:44
  • Just add a control event to your text fields for .EditingDidEnd and define a selector method where you can do all your calculation there before calling tableview.reloadData() – Leo Dabus Jun 05 '16 at 19:51
  • http://stackoverflow.com/a/34941447/2303865 – Leo Dabus Jun 05 '16 at 19:56
  • That answer's code need to be updated, Xcode will complain the selector syntax just let it fix it – Leo Dabus Jun 05 '16 at 20:00
  • Im still confused. I have this CustomTableViewCell. Should I not have that? I dont know where to put the control event or where to define the selector, or even where to call the reloadData(). And when I do, I still dont think i'd know what text field in what row is being edited or how to change the others based off what was changed. I might need to be walked through this. – Kevin Corcoran Jun 05 '16 at 22:15

0 Answers0