简体   繁体   中英

Array appending

I'm trying to add the user input from the text field to the array so that i can display those on the table view but unfortunately i am not getting the proper data into the array from the text field. I don't want to use two view controllers, I think it's possible to do it with one view controller with a tableview embedded as you can see in my code. I am new to Swift. How can i solve it?

import UIKit

class ViewController: UIViewController,UITableViewDelegate ,  UITableViewDataSource{

var Dataceiver : [Data] = []
var todoItem: Data  = Data(tasksName: "")

@IBOutlet var taksTextField: UITextField!
@IBOutlet var tasksTableview: UITableView!
@IBOutlet var taskButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    loadInitialData()
}

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

func loadInitialData() {
    Dataceiver = [
        Data(tasksName: "Go to the dentist"),
        Data(tasksName: "Fetch groceries"),
        Data(tasksName: "Sleep")
    ]
}

@IBAction func tasksButtonTapped(sender: UIButton) {

    self.todoItem = Data(tasksName: self.taksTextField.text!)
    self.Dataceiver.append(todoItem)

}

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


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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let tempCell = tableView.dequeueReusableCellWithIdentifier("ListPrototypeCell")! as UITableViewCell

let todoItem2 = Dataceiver[indexPath.row]

// Downcast from UILabel? to UILabel
let cell = tempCell.textLabel as UILabel!
cell.text = todoItem2.tasksName

//check if complete for the check mark
if (todoItem2.complete) {
    tempCell.accessoryType = UITableViewCellAccessoryType.Checkmark;
} else {
    tempCell.accessoryType = UITableViewCellAccessoryType.None;
}

return tempCell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: false)
    let tappedItem = Dataceiver[indexPath.row] as Data
    tappedItem.complete = !tappedItem.complete

    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}

}

Call tableView.reloadData() after loadInitialData() to refresh the tableView.

Why you need to call reloadData() in UITableView:

UITableView overrides the layoutSubviews method of UIView so that it calls reloadData only when you create a new instance of UITableView or when you assign a new data source. Reloading the table view clears current state, including the current selection. However, if you explicitly call reloadData, it clears this state and any subsequent direct or indirect call to layoutSubviews does not trigger a reload.

Refer to Apple Docs

in tasksButtonTapped() you need to reload table after self.Dataceiver.append(todoItem)

@IBAction func tasksButtonTapped(sender: UIButton) {

    self.todoItem = Data(tasksName: self.taksTextField.text!)
    self.Dataceiver.append(todoItem)
    self.tasksTableview.reloadData()

}

after adding values in array the table still has old values, you need to refres the table so that it can take new values which were added in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM