XML Parser not organizing data into tables - swift

I'm creating an app that involves parsing XML files of a website and putting the information into a table view. Currently the storyboard stands as a tabbed view application with 4 table view controllers connecting to it. I have developed the accompanying class file and set one of the controllers to run off of it. However, the class, while it gives no error, is not returning and creating any tables. The screen is blank just as before there were any swift files. The file is shown below
import UIKit
class Newspage: UITableViewController, NSXMLParserDelegate
{
var parser: NSXMLParser = NSXMLParser()
var info: [newsarticle] = []
var postTitle: String = String()
var postDesc: String = String()
var eName: String = String()
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
let url:NSURL = NSURL(string: "http://brrsd.k12.nj.us/rss/News.xml")!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI namespaceURI: String?, qualifiedName qualifiedName: String?, attributes attributeDict: [String : String])
{
eName = elementName
if elementName == "item" {
postTitle = String()
postDesc = String()
}
}
func parser(parser: NSXMLParser!, foundCharacters string: String!) {
let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if (!data.isEmpty) {
if eName == "title" {
postTitle += data
} else if eName == "description" {
postDesc += data
}
}
}
func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
if elementName == "item" {
let newsart: newsarticle = newsarticle()
newsart.title = postTitle
newsart.description = postDesc
info.append(newsart)
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return info.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let news: newsarticle = info[indexPath.row]
cell.textLabel!.text = news.title
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .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
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Where is the error?
Note: the newsarticle class just contains 2 variables, title and description.

I think your problem is you do not ask your table to refresh itself when you're done parsing the XML.
you need to tell the table to refresh itself when you want it to, by using tableView.reloadData().
You can do this at the end of each element or when the parse is ended by calling reloaData in the delegate function parserDidEndDocument(_ parser: NSXMLParser).
Also, you should not need to call two times super.viewDidLoad() in viewDidLoad.
I'd be you I also would separate the viewController class and the parser delegate in two independent classes.
your cellForRowAtIndexPath is also bothering me:
I don't think you need the second argument (indexPath) to get the cell. You don't want to get the cell a IndexPath but to return a cell to put at IndexPath.
tableView.dequeueReusableCellWithIdentifier("Cell") should get you a cell with the 'Cell' identifier that is available for reuse. For exemple when you scroll down the cell on the top that are not displayed anymore get reused for display on the bottom.
here is how I would implement this function
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
if let reusedCell = tableView.dequeueReusableCellWithIdentifier("Cell") {
cell = reusedCell
} else {
cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
}
if let label = cell.textLabel {
let news = info[indexPath.row]
label.text = news.title
}
return cell
}

Related

toggle button with boolean in UITableView Swift

I would like to add a boolean toggle to know whether the button has been pressed (will save this value to core data (also would like to save cell data to core data if true and delete from core data if false)) I am not sure how to do this. any help would be greatly appreciated. if all the code from the view controller is required leave a comment and I will do so (I have setup xcdatamodel already).
#IBAction func buttonTapped(_ sender:UIButton) {
var superView = sender.superview
while !(superView is UITableViewCell) {
superView = superView?.superview
}
let cell = superView as! UITableViewCell
if let indexpath = tableView.indexPath(for: cell){
print(indexpath)
}
}
Well I build a simple project and I figured something out using protocols,
First you define a protocol like this:
protocol cellDidRequestSaving {
func saveOrDelete(indexpath : Int)
}
First in your cell you define you button like this :
class TableViewCell: UITableViewCell {
var delegate: cellDidRequestSaving?
var indexPath = 0 //come from the parent
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
#IBAction func didTap(_ sender: Any) {
// this protocol defined in the parent
delegate?.saveOrDelete(indexpath: indexPath)
}
}
now in you tableViewController you use the protocol like this:
class TableViewController: UITableViewController, cellDidRequestSaving {
var cellStat = [Int:Bool]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.indexPath = indexPath.row
cell.delegate = self
// Configure the cell...
return cell
}
func saveOrDelete(indexpath: Int) {
if let status = cellStat[indexpath], status {
print(indexpath, "delete")
cellStat[indexpath] = !status
}
else {
print(indexpath, "save")
cellStat[indexpath] = true
}
}
This is a simple project but you can get the hole idea about how to do it. Also, notice the protocol definition and usage so you wont miss anything.
and the out put is this
Add a property to your custom cell, for example:
var indexPath: IndexPath = IndexPath(row: -1, section: -1) //The -1 just know when it is not set yet
And in cellForRow in your tableView:
cell.indexpath = indexPath
Supposing that buttonTapped is defined in your custom cell class:
#IBAction func buttonTapped(_ sender:UIButton) {
print(indexpath)
}
Add a delegate method to your cell - (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button
So then in your VC you can work backwards from the cell object.
- (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
MyObject *object = [self.dataSource objectAtIndexPath:indexPath];
// Do something with the knowledge that the button in the cell for the object was tapped...
// For example
object.tapped = YES;
[object.managedObjectContext save:NULL];
}
Just need to convert to swift ;)

What identifier should I add for this optional?

I want to add this optional
var mapRegion : MKCoordinateRegion
It gives me an error that I need an identifier next to class:
class TableViewController: UITableViewController, NSFetchedResultsControllerDelegate,
What's that identifier?
The file looks like this:
import UIKit
import CoreData
import MapKit
class TableViewController: UITableViewController, NSFetchedResultsControllerDelegate, {
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var frc : NSFetchedResultsController = NSFetchedResultsController()
var mapRegion : MKCoordinateRegion
func fetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Item")
let sortDescriptor = NSSortDescriptor(key: "name",
ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
func getFRC() -> NSFetchedResultsController {
frc = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
return frc
}
override func viewDidLoad() {
super.viewDidLoad()
frc = getFRC()
frc.delegate = self
do {
try frc.performFetch()
} catch {
print("Failed to perform initial fetch.")
return
}
self.tableView.rowHeight = 480
self.tableView.backgroundView = UIImageView( image: UIImage(named: "flatgrey2"))
self .tableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
frc = getFRC()
frc.delegate = self
do {
try frc.performFetch()
} catch {
print("Failed to perform initial fetch.")
return
}
self .tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
let numberOfSections = frc.sections?.count
return numberOfSections!
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
let numberofRowsInSection = frc.sections?[section].numberOfObjects
return numberofRowsInSection!
//return 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor.clearColor()
} else {
cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)
cell.textLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
cell.detailTextLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
}
// Configure the cell...
cell.textLabel?.textColor = UIColor.darkGrayColor()
cell.detailTextLabel?.textColor = UIColor.darkGrayColor()
let item = frc.objectAtIndexPath(indexPath) as! Item
cell.textLabel?.text = item.name
cell.imageView?.image = UIImage(data: (item.image)!)
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let managedObject : NSManagedObject = frc.objectAtIndexPath(indexPath) as! NSManagedObject
moc.deleteObject(managedObject)
do {
try moc.save()
} catch {
print ("Failed to save.")
return
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
tableView.reloadData()
}
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "edit" {
let cell = sender as! UITableViewCell
let indexPath = tableView.indexPathForCell(cell)
let itemController : AddEditVC = segue.destinationViewController as! AddEditVC
let item : Item = frc.objectAtIndexPath(indexPath!) as! Item
itemController.item = item
}
}
}
The variable should have a value when it is declared otherwise it will show this error. Assign a value to your variable var mapRegion : MKCoordinateRegion. If the value is given at a later stage i.e., if it depends on any calculations then make it an optional like this var mapRegion : MKCoordinateRegion? But you have to unwrap the variable whenever you want to use mapRegion value by placing an exclamation mark at the end of the variable name.
You have declared var mapRegion : MKCoordinateRegion but don't have any initializers or default value. To make it an optional, just add ? to it, so declare:
var mapRegion : MKCoordinateRegion?

creating searchviewcontroller in parse.com table

I have created a basic layout for searchViewcontroller in which I enter a data that I want to search inside parse table as a query and search is completed and the cell appears. I have tried searching but various errors are appearing. I have no clue to do this.
I'm trying to create an independent ViewController for search.
class SearchViewController: PFQueryTableViewController, UISearchBarDelegate {
// Table search bar
#IBOutlet weak var searchBar: UISearchBar!
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
// Configure the PFQueryTableView
self.parseClassName = "Countries"
self.textKey = "nameEnglish"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
if let nameEnglish = object?["nameEnglish"] as? String {
cell.customNameEnglish.text = nameEnglish
}
if let capital = object?["capital"] as? String {
cell.customCapital.text = capital
}
// Display flag image
var initialThumbnail = UIImage(named: "question")
cell.customFlag.image = initialThumbnail
if let thumbnail = object?["flag"] as? PFFile {
cell.customFlag.file = thumbnail
cell.customFlag.loadInBackground()
}
return cell
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
var detailScene = segue.destinationViewController as! DetailViewController
// Pass the selected object to the destination view controller.
if let indexPath = self.tableView.indexPathForSelectedRow! {
let row = Int(indexPath.row)
detailScene.currentObject = objects?[row] as? PFObject
}
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
// Start the query object
let query = PFQuery(className: "Countries")
// Add a where clause if there is a search criteria
if searchBar.text != "" {
query.whereKey("searchText", containsString: searchBar.text!.lowercaseString)
}
// Order the results
query.orderByAscending("nameEnglish")
// Return the qwuery object
return query
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
// Dismiss the keyboard
searchBar.resignFirstResponder()
// Force reload of table data
self.loadObjects()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
// Dismiss the keyboard
searchBar.resignFirstResponder()
// Force reload of table data
self.loadObjects()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// Clear any search criteria
searchBar.text = ""
// Dismiss the keyboard
searchBar.resignFirstResponder()
// Force reload of table data
self.loadObjects()
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
tableView.reloadData()
// Delegate the search bar to this table view class
searchBar.delegate = self
}
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
let objectToDelete = objects?[indexPath.row] as! PFObject
objectToDelete.deleteInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// Force a reload of the table - fetching fresh data from Parse platform
self.loadObjects()
} else {
// There was a problem, check error.description
}
}
} 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
}
}
}
If any clue is provided I'll be grateful.
This code is in reference with Bizzi-Body Parse Tutorial .

Not able to access the variable declared outside the function in UITableviewcontroller swift

I am using a simple UITableViewController
var testdata = [String]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("controlcell",
forIndexPath: indexPath) as! TableViewCell
// Configure the cell...
cell.clabel.text = testdata[indexPath.row]
return cell
}
testdata is declared outside the function. testdata is also populated from another method.
The issue is: self.testdata shows no data when I put a breakpoint and check the data.
Am I using the variable correctly in swift ?
Adding complete code here:
import UIKit
import Alamofire
class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
struct ListItem {
var Name:String = ""
var Number:String = ""
var Email:String = ""
}
override func viewDidLoad() {
super.viewDidLoad()
getjsonData()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
// return controlstaticdata.count
// return controlListData.controlListItems.count
return testdata.count
}
var testdata = [String]()
func getjsonData(){
var jsondata:NSData!
let url = "http://localhost:3000/controllistapp"
Alamofire.request(.GET, url).responseJSON(){
(_,_,data,_) in
println(data)
// parse the jsondata returned by Alamofire .GET request
let json = JSON(data!)
var i = 0
for(_, subJson): (String, JSON) in json {
if let Name = subJson["Name"].string,
let Number = subJson["Number"].string,
let Email = subJson["Email"].string{
var item = ListItem(Name: Name, Number: Number, Email: Email)
self.testdata += [Name]
}
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("controlcell", forIndexPath: indexPath) as! TableViewCell
// Configure the cell...
cell.clabel.text = self.testdata[indexPath.row]
return cell
}
}
A breakpoint at line self.testdata += [Name] in getjsonData shows the data being populated.
However, a breakpoint at line cell.clabel.text = self.testdata[indexPath.row] in tableView function shows there is no data and throws an exception.
I think your problem is that you forgot to include self.tableView.reloadData() after you populate data into the array

Saving values to UserDefults from Textfield in TableView : Swift

I am trying to save values from a tableview that users will input into the textfield, the problem is that I do not know how to access the new value and replace the string in the array.
So basically the app will display fields based on what the user wants and then the user can edit those values to their liking. Once the textfields have been updated, the values are stored again in userdefaults so that the next time the tableview is opened, the update values will appear.
This is what the tableviewcontroller looks like at the moment:
//
// Asset1TableViewController.swift
// Net Calc 2
//
// Created by Joshua Peterson on 30/06/2015.
// Copyright © 2015 Peterson Productions. All rights reserved.
//
import UIKit
class Asset1TableViewController: UITableViewController {
var dataHolder: [NSString] = [NSString]()
var finalDataHolder: [NSString] = [NSString]()
var acountAmountHolder: [NSString] = [NSString]()
var finalAccountAmountHolder: [NSString] = [NSString]()
let defaults = NSUserDefaults.standardUserDefaults()
let key1 = "keySave1"
let key2 = "keySave2"
override func viewDidLoad() {
super.viewDidLoad()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if let storedTitleValue : NSArray? = self.defaults.arrayForKey(self.key1) {
if storedTitleValue == nil {
self.dataHolder = [NSString]()
} else {
let readArray : [NSString] = storedTitleValue as! [NSString]
for element in readArray {
self.dataHolder.append(element as String)
self.finalDataHolder.append(element as String)
}
}
}
if let storedAmountValue : NSArray? = self.defaults.arrayForKey(self.key2) {
if storedAmountValue == nil {
self.acountAmountHolder = [NSString]()
} else {
let readArray : [NSString] = storedAmountValue as! [NSString]
for element in readArray {
self.acountAmountHolder.append(element as String)
self.finalAccountAmountHolder.append(element as String)
}
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataHolder.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Account1Cell", forIndexPath: indexPath) as! Account1Cell
cell.AccountLabel.text = dataHolder[indexPath.row] as String
cell.AccountAmount.text = acountAmountHolder[indexPath.row] as String
return cell
}
#IBAction func addButtonTapped(sender: AnyObject) {
let newAccounTitle = "Account Name"
let newAccountAmount = "R0.00"
dataHolder.append(newAccounTitle)
acountAmountHolder.append(newAccountAmount)
tableView.reloadData()
}
#IBAction func saveButtonTapped(sender: AnyObject) {
// Save
defaults.setObject(dataHolder as Array, forKey: key1)
defaults.setObject(acountAmountHolder as Array, forKey: key2)
defaults.synchronize()
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
dataHolder.removeAtIndex(indexPath.row)
acountAmountHolder.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
}
I have tried to apply some of the code that i have found on the website but the problem is that I cant actually connect to the cell.
Ok so after some research I have added a few functions to the custom cell class so that it looks like this:
import UIKit
protocol TableViewCellDelegate {
// Indicates that the edit process has begun for the given cell
func cellDidBeginEditing(editingCell: Account1Cell)
// Indicates that the edit process has committed for the given cell
func cellDidEndEditing(editingCell: Account1Cell)
}
class Account1Cell: UITableViewCell, UITextFieldDelegate {
#IBOutlet weak var AccountLabel: UITextField!
#IBOutlet weak var AccountAmount: UITextField!
var delegate: TableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
AccountLabel.delegate = self
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
// close the keyboard on Enter
AccountLabel.resignFirstResponder()
return false
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
// disable editing of completed to-do items
return true
}
func textFieldDidEndEditing(textField: UITextField) {
if AccountLabel != nil {
let newAccountLabel = AccountLabel.text
print(newAccountLabel) // Prints out the new edited text!!!!!!!!
}
if delegate != nil {
delegate!.cellDidEndEditing(self)
}
}
func textFieldDidBeginEditing(textField: UITextField) {
if delegate != nil {
delegate!.cellDidBeginEditing(self)
}
}
}
Now what I need to do is either replace that value in the Array at the index (which i think is going to be rather complicated) or create some sort of loop that will read ALL the values and simply store all of the new values to UserDefaults. Maybe there is something else?
Any help is appreciated!!
You should have a protocol in your custom cell like this, and call it when the text field in the cell gets modified:
protocol TableViewCellToTVController{
func cellCurrentlyEditing(editingCell: Account1Cell) -> Int
}
....
func textFieldShouldReturn(textField: UITextField) -> Bool {
// close the keyboard on Enter
let myrow: Int? = self.delegate_special?.cellCurrentlyEditing(self)
println("cellCurrentlyEditing got called from delegate" , myrow)
AccountLabel.resignFirstResponder()
return false
}
implement this function in tableviewcontroller to know which row got selected :
func cellCurrentlyEditing(editingCell: Account1Cell) -> Int{
var rowNum = 0
let indexP: NSIndexPath = tableView.indexPathForCell(editingCell)!
rowNum = indexP.row
return rowNum
}
also make your tableviewcontroller the delegate for each cell:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Account1Cell", forIndexPath: indexPath) as! Account1Cell
cell.AccountLabel.text = dataHolder[indexPath.row] as String
cell.AccountAmount.text = acountAmountHolder[indexPath.row] as String
cell.delegate_special = self;
return cell
}