Ordering TableViews by latest time - ios

Currently I am having trouble ordering my table views in the right order. As of right now, they don't change order no matter what. I'm not sure what to add to fix this.
import UIKit
class ListNotesTableViewController: UITableViewController {
var notes = [Note](){
didSet{
tableView.reloadData();
}
}
override func viewDidLoad() {
super.viewDidLoad()
notes = CoreDataHelper.retrieveNotes()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notes.count;
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath : IndexPath) ->ListNotesTableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "listNotesTableViewCell", for: indexPath) as! ListNotesTableViewCell
let row = indexPath.row
let note = notes[row]
cell.noteTitleLabel.text = note.title
cell.notePreview.text = note.content;
cell.noteModificationTimeLabel.text = note.modificationTime?.convertToString()
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
if identifier == "displayNote" {
print("Table view cell tapped")
let indexPath = tableView.indexPathForSelectedRow!
let note = notes[indexPath.row]
let displayNoteViewController = segue.destination as! DisplayNoteViewController
displayNoteViewController.note = note
} else if identifier == "addNote" {
print("+ button tapped")
}
}
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
CoreDataHelper.delete(note: notes[indexPath.row])
notes = CoreDataHelper.retrieveNotes()
}
}
#IBAction func unwindToListNotesViewController(_ segue: UIStoryboardSegue){
self.notes = CoreDataHelper.retrieveNotes()
}
}

It seems like your notes is not sorted by time. Whenever you get self.notes = CoreDataHelper.retrieveNotes() you need to sort your array. You can do it like this:
self.notes.sorted(by: { $0.date < $1.date })
Note that the date variable can differ depending on what variable name you have for your time and structure on your Note class.

Related

I want to display Array from UITableView to the next viewController using didSelectRowAt

I want the quizNumber to return the quizNumberArray depending on which cell is selected.
For example, if the user selected the first cell the quizNumber returns 1
Inside the UITableViewController
let quizNumberArray = [1,2,3,4,5,6,7,8,9,10]
var quizNum = Int()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "toQuestionVC") as? QuestionVC
quizNum = quizNumberArray[indexPath.row]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
if identifier == "toQuestionVC" {
let questionController = segue.destination as! QuestionVC
questionController.quizNumber = quizNum
}
in the next ViewController
class QuestionVC : UIViewController {
var quizNumber = Int()
func updateQuestion(){
if quizNumber == 1 {
//.....
}
}
}
But the quizNumber return 0.
Try this Code:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "toQuestionVC") as? QuestionVC
vc?.quizNumber = quizNumberArray[indexPath.row]
navigationController?.pushViewController(vc!, animated: true)
}
and remove the segue to work the above code

'else' statement not running in 'didSelectRowAt' function while executing

don't know whether it is common or not but in 'didSelectRowAt' function, 'if' statement is executing perfectly but 'else' statement is not and whenever i tap on the cell the checked property is always true down below is the code which might help :-
here is the code :-
import UIKit
import Alamofire
import SwiftyJSON
class TableViewController: UITableViewController {
var vCData = [Item]()
let url = "https://api.coinmarketcap.com/v1/ticker/"
var items = [Item]()
override func viewDidLoad() {
super.viewDidLoad()
getCoinData(url: url)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
let arrayItem = items[indexPath.row]
cell.textLabel?.text = arrayItem.name
cell.detailTextLabel?.text = arrayItem.symbol
if arrayItem.checked == true{cell.accessoryType = .checkmark}
else{cell.accessoryType = .none}
return cell
}
THIS IS WHERE PROBLEM OCCURS :-
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
var selItem = items[indexPath.row]
if selItem.checked == false {
selItem.checked = true
vCData.append(selItem)
}else { // statement not working
selItem.checked = false
if let index : Int = self.vCData.index(where: {$0.name == selItem.name && $0.checked == selItem.checked && $0.buyPrice == selItem.buyPrice && $0.rank == selItem.rank && $0.symbol == selItem.symbol}) {
self.vCData.remove(at: index)
print(vCData,"\n")
}
}
tableView.reloadData()
}
#IBAction func doneTapped(_ sender: UIBarButtonItem) {
self.dismiss(animated: true, completion: nil)
}
}
Assuming Item is a struct and not a class, your issue is the result of the fact that struct is a value type and copies are made when you assign from one variable to another.
In didSelectRowAt you update selItem but this does not change the copy stored in items.
The fix is simple. After your if/else, update items with the new selItem:
items[indexPath.row] = selfItem
On an unrelated note, I would replace the use of reloadData in didSelectRowAt with:
tableView.reloadRows(at: [ indexPath ], with: .fade)
No need to reload the whole table just to update one row.

SWIFT 4 Detail Disclosure

I'm new to programming and have just started with a deep dive into SWIFT and iOS development.
I've been stuck on a problem for over a few hours now and would be much obliged if someone could answer it. The assignment is bucket list and we have to built a basic app that uses two Table views (one dynamic and one static) to display a list of "bucket list" items using navigator controllers and modal display. In our first assignment, we were allowed to use two segues for the edit and save functionality but in the new assignment, we are being asked to refactor and use one segue with the hint being that we should use sender information. The present code uses two segues as I have not figured out how to refactor. As a note, we are using the accessory Detail Disclosure to bring up list items for edit. This is where I am getting stuck. From what I've been able to read, I understand the Disclosure Detail button is not a "true button" rather it is part of the index path. While I think I understand what needs to be done from a conceptual level, I am having a hell of a time actually solving the problem. Thanks in advance.
import UIKit
class BucketListViewController: UITableViewController,
AddItemTableViewControllerDelegate {
var items = ["awesome","lessawesome","great"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
cell.textLabel?.text = items[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected")
}
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
performSegue(withIdentifier: "EditItemSegue", sender: indexPath)
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
items.remove(at: indexPath.row)
tableView.reloadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddItemSegue"{
let navigationController = segue.destination as! UINavigationController
let addItemTableViewController = navigationController.topViewController as! AddItemTableViewController
addItemTableViewController.delegate = self
}else if segue.identifier == "EditItemSegue"{
let navigationController = segue.destination as! UINavigationController
let AddItemTableViewController = navigationController.topViewController as! AddItemTableViewController
AddItemTableViewController.delegate = self
let indexPath = sender as! NSIndexPath
let item = items[indexPath.row]
AddItemTableViewController.item = item
AddItemTableViewController.indexPath = indexPath
}
}
func cancelButtonPressed(by controller: AddItemTableViewController) {
dismiss(animated: true, completion: nil)
}
func itemSaved(by controller: AddItemTableViewController, with text:String, at indexPath: NSIndexPath?) {
if let ip = indexPath {
items[ip.row] = text
}else{
items.append(text)
}
tableView.reloadData()
dismiss(animated: true, completion: nil)
}
}
AddItemTableViewControllerDelegate
import Foundation
import UIKit
protocol AddItemTableViewControllerDelegate: class {
func itemSaved(by controller: AddItemTableViewController, with text:String, at indexPath: NSIndexPath?)
func cancelButtonPressed(by controller: AddItemTableViewController)
}
AddItemTableViewController
import UIKit
class AddItemTableViewController: UITableViewController {
weak var delegate: AddItemTableViewControllerDelegate?
var item: String?
var indexPath: NSIndexPath?
#IBOutlet weak var itemTextField: UITextField!
#IBAction func cancelButtonPressed(_ sender: UIBarButtonItem) {
delegate?.cancelButtonPressed(by: self)
}
#IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
let text = itemTextField.text!
delegate?.itemSaved(by: self, with: text, at: indexPath)
}
override func viewDidLoad() {
super.viewDidLoad()
itemTextField.text = item
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Editing table row cell swift 3

I am trying to edit the table row cell. I have different tableViewController set up to display/edit the current events when they touch on the displayed events from initial tableViewController. Whenever I edit an event and hit save, it goes back to the main viewController where i am initially displaying my events, but it doesn't display the edited events. it keeps showing the original events.
Here is the portion of my main tableView file where i am displaying my events:
class EventsTable: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableData = ViewController()
#IBOutlet weak var table: UITableView!
#IBAction func saveToMainViewController (change:UIStoryboardSegue) {
let editViewController = change.source as! EditEventsTableViewController
let index = editViewController.index
let titleString = editViewController.editedTitle
tableData.eventsArray[index!].title = titleString
table.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return tableData.eventsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomeCell
cell.eventTitle.text = tableData.eventsArray[indexPath.row].title
cell.eventLocation.text = tableData.eventsArray[indexPath.row].location
cell.eventDateAndTime.text = tableData.eventsArray[indexPath.row].date
return cell
}
//function to delete cell and saves it
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableData.eventsArray.remove(at: indexPath.row)
table.reloadData()
let savedEvents = NSKeyedArchiver.archivedData(withRootObject: tableData.eventsArray)
UserDefaults.standard.set(savedEvents, forKey: "savedEvents")
UserDefaults.standard.synchronize()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "edit" {
var path = table.indexPathForSelectedRow
let detailViewController = segue.destination as! EditEventsTableViewController
detailViewController.index = path?.row
detailViewController.eventsArray = tableData.eventsArray
}
}
Here is the other tableViewController where i edit my data
class EditEventsTableViewController: UITableViewController {
#IBOutlet weak var txtEditTitle: UITextField!
var index:Int!
var eventsArray = [Event]()
var editedTitle: String!
override func viewDidLoad() {
super.viewDidLoad()
txtEditTitle.text = eventsArray[index!].title
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 && indexPath.row == 0 {
txtEditTitle.becomeFirstResponder()
}
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "save" {
editedTitle = txtEditTitle.text
}
}
}
incase you guys need a better look at the project,
download link for the project
Probably in EditEventsTableViewController you missed table view reloading?
override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
txtEditTitle.text = eventsArray[index!].title
}
It's unclear, where do you provide data for EditEventsTableViewController. Is it static table view, designed in storyboard?

TableView is Nil

I have the following class:
import UIKit
import CloudKit
class FirstViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var listTableView: UITableView!
var list: TLListModel!
var specificList: CKRecord!
override func viewDidLoad()
{
super.viewDidLoad()
let myContainer = CKContainer.default()
list = TLListModel(container: myContainer, viewController: self)
if(listTableView != nil){
listTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("number of items: %i", list.lists.count)
return list.lists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = listTableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
let list: CKRecord = self.list.lists[(indexPath as NSIndexPath).row]
cell.textLabel?.text = list.value(forKey: "ListName") as? String
cell.textLabel?.font = UIFont (name: "Slim Joe", size: 20)
cell.accessoryType = .disclosureIndicator
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("This object has been selected")
print(self.list.lists[(indexPath as NSIndexPath).row])
specificList = self.list.lists[(indexPath as NSIndexPath).row]
performSegue(withIdentifier: "TLSpecificListSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TLSpecificListSegue"{
if let destinationVC = segue.destination as? TLSpecificListViewController{
destinationVC.listObject = specificList
}
}
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == .delete
{
let cloudkit = TLCloudKitHelper()
cloudkit.deleteListItem(self.list.lists[(indexPath as NSIndexPath).row], callback: { (listName) in
TLAlertHelper.notifyUser("List Deleted", message: NSString(format: "List for %# successfully deleted", listName) as String, sender: self)
let myContainer = CKContainer.default()
self.list = TLListModel(container: myContainer, viewController: self)
DispatchQueue.main.async {
self.listTableView.reloadData()
}
})
}
}
}
When I call it from another view controller using the following method:
#IBAction func createListAction(_ sender: AnyObject) {
let cloudkit = TLCloudKitHelper()
let listArray = createListFromTextField(textInputArea.text)
if(!(listNameTextField.text?.isEmpty)!){
cloudkit.createList(listNameTextField.text!) { (response) in
let listId = response
if (!listArray.isEmpty){
for item in listArray{
cloudkit.saveItemRecord(item, listId: listId, recordName: response)
}
}
let fvc: FirstViewController = FirstViewController()
DispatchQueue.main.async {
self.present(fvc, animated: true, completion: nil)
}
}
}else{
TLAlertHelper.notifyUser("Give the list a name", message: "You need to give you list a name...", sender:self)
}
}
I get an error saying fatal error: unexpectedly found nil while unwrapping an Optional value
I don't understand why I am getting this error. I've tried looking at the answers here: Simple UITableView in Swift - unexpectedly found nil but I none of those answers helped. Can someone tell me why this this crashing and how I can fix it?
The problem is this line:
let fvc: FirstViewController = FirstViewController()
This creates a blank FirstViewController instance — one completely unconnected with the interface you designed in the storyboard. Its view is empty. It has no table view in it. Therefore, since there is no table view, there is no outlet connection from any table view, and listTableView remains nil.
What you want to do is get the FirstViewController instance from the storyboard, the one whose interface you have already designed in the storyboard. You can do that by talking to the storyboard and using the FirstViewController's identifier, i.e., call instantiateViewController(withIdentifier:). (You might have to give the FirstViewController in the storyboard an identifier for this purpose.)
EDIT This is such a common mistake that I've written a blog post about it: http://www.programmingios.net/dont-make-a-new-instance-by-mistake/

Resources