Allow me to provide some context. I have a UITableViewCell that contains an EditText and a Button. This cell is then inserted into the self.tableView.tableHeaderView
property of the table to achieve the following result:
Below is my implementation for the view controller:
class ViewController: UITableViewController {
var data = ["Apple", "Apricot"] as NSMutableArray
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Testing"
createHeader()
}
func createHeader(){
let headerViewCell = NSBundle.mainBundle().loadNibNamed("InputToListCell", owner: nil, options: nil)[0] as! InputToListCell
headerViewCell.setTableViewWithData(self,data: data)
self.tableView.tableHeaderView = headerViewCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("rowCell", forIndexPath: indexPath)
cell.textLabel?.text = data[indexPath.row] as? String
return cell
}
}
Here is my implementation for the UITableViewCell that is inserted into the table:
class InputToListCell: UITableViewCell {
@IBOutlet weak var inputEditText: UITextField!
var tableViewController: ViewController!
var arrayForTable: NSMutableArray!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setTableViewWithData(inputTableViewController:ViewController, data:NSMutableArray){
tableViewController = inputTableViewController
arrayForTable = data
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func addValueToArray(sender: AnyObject) {
let inputFromEditText = inputEditText.text!
arrayForTable.addObject(inputFromEditText)
tableViewController.data = arrayForTable
tableViewController.tableView.reloadData()
tableViewController.createHeader()
}
}
I have just started with iOS development and I dont have access to a senior developer to review my code to see if things could be improved or done in a better/best practices sort of way. Any help or suggestions would be great.