I'm making a to-do list app but when I try to delete something from my list, xcode is giving me an error that says "fatal error: array index out of range". Can someone tell me what I'm doing wrong with my array that is causing this to happen?
import UIKit
class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
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.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = eventList[indexPath.row]
return cell
}
override func viewWillAppear(animated: Bool) {
if var storedEventList : AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("EventList") {
eventList = []
for var i = 0; i < storedEventList.count; ++i {
eventList.append(storedEventList[i] as NSString)
}
}
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == UITableViewCellEditingStyle.Delete) {
eventList.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(eventList, forKey: "EventList")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
A breakpoint saying EXC_BAD_INSTRUCTION is being created at the eventList.removeAtIndex(indexPath.row)as well.
It is not sufficient to remove the item from the data source array.
You also have to tell the table view that the row is deleted:
if editingStyle == .Delete {
eventList.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
// ...
}
Otherwise the table view will call the data source methods for the original
number of rows, causing the out of range error.
Alternatively, you can call tableView.reloadData() when modifying the data source, but the above method
gives a nicer animation.
It means you are trying to access an index,indexPath.row, that exceed the eventList range. To fix this issue try:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == .Delete && indexPath.row < eventList.count) {
eventList.removeAtIndex(indexPath.row)
tableView.reloadData()
NSUserDefaults.standardUserDefaults().setObject(eventList, forKey: "EventList")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
Related
class showPageViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var records : [Record] = []
#IBOutlet weak var tableView: UITableView!
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.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return records.count
}
func tableview(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func tableView(_ tableVoew: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if editingStyle == .delete{
let record = records[indexPath.row]
context.delete(record)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do{
records = try context.fetch(Record.fetchRequest())
} catch{
print("Failed")
}
}
}
override func viewWillAppear(_ animated: Bool) {
getData()
tableView.reloadData()
}
func getData(){
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do{
records = try context.fetch(Record.fetchRequest())
} catch{
print("123")
}
}
}
This is the code of my program, I am trying to make a to-do list app, but the error does not confirm to protocol "UITableViewDataSource" always appear, I try to find the solution, but nothing suitable for me, is anyone can help me? thanks
You have a mix of Swift 2 and Swift 3 methods in your code. Your cellForRow... method is using the newer Swift 3 signature. You need to use the older Swift 2 compatible version.
I believe it is:
func tableview(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
Check all of the other table view data source and delegate methods and make sure they all use the Swift 2 compatible signatures and not the newer Swift 3 signatures.
Yes, You are mixing swift 2 and swift 3. Your methods should be like this:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Configure the cell...
return cell
}
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
}
}
You have done several spelling mistakes in those methods, Please check above methods with yours
I'm making a notes app and added the swipe to delete a row method. The problem I'm having is when there is multiple notes saved in the table view and I go to swipe one row to delete it deletes all the notes. Also when I quit the app and go back the notes are back in the table view. Heres the code I have:
class MasterViewController: UITableViewController {
var notesItems: NSMutableArray = NSMutableArray()
override func viewDidAppear(animated: Bool) {
let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let itemListFromUserDefaults:NSMutableArray? = userDefaults.objectForKey("itemList") as? NSMutableArray
if ((itemListFromUserDefaults) != nil) {
notesItems = itemListFromUserDefaults!
}
self.tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.toolbarHidden = false
self.tableView.dataSource = self
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UIToolbar.appearance().barTintColor = UIColor.orangeColor()
}
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notesItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let notesItem:NSDictionary = notesItems.objectAtIndex(indexPath.row) as! NSDictionary
cell.textLabel?.text = notesItem.objectForKey("text") as? String
return cell
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
self.tableView.reloadData()
self.tableView.beginUpdates()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
// Delete the row from the data source
}
}
}
Your code in commitEditingStyle is all wrong.
Don't reload the table view.
You must update the database before calling deleteRowsAtIndexPaths.
You don't need to call beginUpdates/endUpdated to make one call to deleteRowsAtIndexPaths.
You want:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
// remove an object from notesItem for this index path
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}
Currently, I have a to-do list app. When selecting a row, the alpha dims, indicating that the task is selected or "completed". I have been searching vigorously on here how to save the selected cell state to NSUserDefaults.
My ViewController:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var defaults = NSUserDefaults.standardUserDefaults()
#IBOutlet weak var toDoListTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if defaults.objectForKey("toDoList") != nil {
toDoList = defaults.objectForKey("toDoList") as [String]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toDoList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.alpha = 0.3
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var deselectedCell = tableView.cellForRowAtIndexPath(indexPath)!
deselectedCell.contentView.alpha = 1.0
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
toDoList.removeAtIndex(indexPath.row)
toDoListTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
defaults.setObject(toDoList, forKey: "toDoList")
}
}
override func viewDidAppear(animated: Bool) {
toDoListTable.reloadData()
}
}
I believe I'm just having an issue calling it/trying to figure out where to call it at. Any help would be greatly appreciated.
EDIT** What I have right now saves the content stored from a UITextField.
Is the problem that it doesn't save?
You can try calling synchronize to save it disk immediately to see if that makes a difference, although it shouldn't be required.
NSUserDefaults.standardUserDefaults().setObject(indexPath.row, forKey: "selection")
NSUserDefaults.standardUserDefaults().synchronize()
NSIndexPath's row/item property is an NSInteger. This is not an object. Use
setInteger(_:forKey:) instead.
#Van Du Tan's answer for defaults.setValue(indexPath.row, forKey: "selection") may not work because you have to encapsulate the row inside an NSValue object.
First off, I'm going to assume you have a line declaring NSUserDefaults:
let defaults = NSUserDefaults.standardUserDefaults()
I believe what your trying to do is store the alpha (assuming state) with respect to the row your selecting. In such a case
let key = "myTableViewWithRowOf\(indexpath.row)"
defaults.setFloat(selectedCell.contentView.alpha, forKey: key)
You can then retrieve this data by using the floatForKey method.
Ideally though you might want to end up with an NSArray associated with your tableView and store that using defaults.setObject(...). That way when your app loads you can call the whole array and loop through it restoring the "states".
I get this error when I actually run the code (this line is the problem var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell. THis is in a UITableViewController class.
import UIKit
class AlarmsTableViewController: UITableViewController {
var myData:Array<AnyObject> = []
override func viewDidLoad() {
myData = ["one", "two", "three", "four"]
}
override func didReceiveMemoryWarning() {
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let CellId:String = "Cell"
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell
//if let ip = indexPath {
cell.textLabel?.text = myData[indexPath.row] as? String
//}
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
//Delete row from data source
//if let tv = tableView? {
myData.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
}
}
}
In the interface builder, select the TableViewController with which you are working with. Then, select the prototype cell and set its style to "Basic". Finally, set the cells reuse identifier to Cell.
I am trying to delete a table view cell using swipe to delete, but I can't use the line I marked in my code, not sure why. My cells are being made by a Dictionary, with the key for the title and the value for the details.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
//Create the cell by the dictionary favDict
for (key, value) in favDict{
cell.textLabel?.text = key
cell.detailTextLabel?.text = value
}
return cell
}
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if editingStyle == UITableViewCellEditingStyle.Delete {
favDict.removeAtIndex(indexPath!.row)// Line that is giving the error says Int is not convertible for [String: String]
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
Resolution:
import UIKit
class ViewController2: UIViewController, UITableViewDelegate {
var dictKeys : [String]?
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
dictKeys = Array(favDict.keys)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dictKeys = Array(favDict.keys)
return rowsInSec
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var key = dictKeys?[indexPath.row]
var data = favDict[key!]
cell.textLabel?.text = key
cell.detailTextLabel?.text = data
return cell
}
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if editingStyle == UITableViewCellEditingStyle.Delete
{
var key = dictKeys?[indexPath.row]
favDict.removeValueForKey(key!)
dictKeys?.removeAtIndex(indexPath.row)
rowsInSec--
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
}
You can't use;
favDict.removeAtIndex(indexPath!.row)
removeAtIndex is a instance method of NSMutableArray. For removing value from dictionary you can use:
favDict.removeValueForKey("yourkey")
EDIT:
The removing and loading data using Dictionary is little bit hard compared to NSMutableArray. Also you put a loop in the cellForRowAtIndexPath: . It'll load only the last object to every cell.
If you are still looking for Dictionary, you can do it like:
Implement the methods like:
var dictKeys : [Strings]?
override func viewDidLoad()
{
super.viewDidLoad()
dictKeys = Array(favDict.keys)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var key = dictKeys?[indexPath.row]
var data = favDict[key!]
cell.textLabel?.text = key
cell.detailTextLabel?.text = data
return cell
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
var key = dictKeys?[indexPath.row]
favDict.removeValueForKey(key!)
dictKeys?.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}