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)
}
}
Related
I am trying to create a simple todo list app for learning purposes i. I want to be able to click on a row and add a check mark and when clicked again i want it to go away. i have looked at several other examples but nothing is working. How can i achieve this?
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tbView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tbView.reloadData()
}
override func viewWillAppear(animated: Bool) {
self.tbView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return tasks.manager.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")
//Assign the contents of our var "items" to the textLabel of each cell
cell.textLabel!.text = tasks.manager[indexPath.row].name
cell.detailTextLabel!.text = tasks.manager[indexPath.row].time
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.Delete){
tasks.manager.removeAtIndex(indexPath.row)
tbView.reloadData()
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell!.accessoryType = .Checkmark
tableView.reloadData()
}
}
To put it simply, you can use this code.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath)
if (cell!.accessoryType == .Checkmark) {
cell!.accessoryType = .None
} else {
cell!.accessoryType = .Checkmark
}
tableView.reloadData()
}
This way, when you re-select cell, the checkmark will adjust accordingly.
However, you shouldn't modify cell checkmark this way as the cell will get re used when you scroll.
You need to update your data model instead it above approach.
so in your data model, add new property to hold checkmark state and then use it in cellForRowAtIndexPath function.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")
//Assign the contents of our var "items" to the textLabel of each cell
cell.textLabel!.text = tasks.manager[indexPath.row].name
cell.detailTextLabel!.text = tasks.manager[indexPath.row].time
if tasks.manager[indexPath.row].isSelected { //assume that isSelected is bool
cell!.accessoryType = .Checkmark
} else {
cell!.accessoryType = .None
}
return cell
}
and then in your didSelectRowAtIndexPath update the model.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let dataModel = tasks.manager[indexPath.row]
dataModel.isSelected = !dataModel.isSelected
tableView.reloadData()
}
I'm assuming that you are using a table view in a storyboard. If that's the case, in the attributes inspector, you can choose a chick mark as the style.
You'll need both didSelectRowAtIndexPath and didDeselectRowAtIndexPath methods of the UITableViewDelegate protocol.
You can simple use them like this!
Deselect
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell!.accessoryType = .None
//tableView.reloadData()
}
Select
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell!.accessoryType = .Checkmark
//tableView.reloadData()
}
I have UIViewController and I dragged a UITableView inside and I want to insert some elements to the table and be able to remove them here . When I use this code it works fine for UITableViewController but now when I want to remove a cell I can swap it but the deleting animation (Red sign) doesn't show up
import UIKit
class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {
var listArray = ["Danial"]
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.
}
#IBOutlet var table: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.textLabel?.text = listArray[indexPath.row]
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){
listArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) }
}
}
The problem with your code is that you are typing:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){
listArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) }
}
instead of
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){
listArray.removeAtIndex(indexPath.row)
self.table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) }
}
so just go to everywhere where you wrote
tableView.
and change it to self.table.
(basically what you named your tableview from the IBOutlet)
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)
}
}
I want to delete cell from the parse server so can anybody tell what should I write under func commiteditingstyle?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let logCell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
let Log:PFObject = self.LogData.objectAtIndex(indexPath.row) as! PFObject
logCell.textLabel?.text = Log.objectForKey("Weight") as? String
return logCell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// delete object from parse, remove from list
}
}
When you wish to delete an object from the Parse cloud, the method deleteInBackground() should be used.
let Log:PFObject = self.LogData.objectAtIndex(indexPath.row) as! PFObject
Log.deleteInBackground()
You'd also want to remove the cell from the tableView itself, if isn't done automatically. You'd need to delete the PFObject from LogData first, and then reload the table. Good luck.
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.