Delete is not working in case of UITableview - ios

MyTableviewController
var animalNameArray = ["cat","dog","lion"]
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
self.cancelButton.isEnabled = false
}
#IBAction func editButtonAtNavigationBar(_ sender: UIBarButtonItem) {
self.cancelButton.isEnabled = true
self.tableview.isEditing = !self.tableview.isEditing
sender.title = (self.tableview.isEditing) ? "Done" : "Edit"
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
animalNameArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .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.
}
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let remove = UITableViewRowAction(style: .default, title: " ") { action, indexPath in
}
remove.backgroundColor = UIColor(patternImage: UIImage(named: "delete")!)
return [remove]
}
//conditional Rearranging the table view cells
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
}
I want to put the image (trash) instead of text, image is showing but the delete action is not happening when trying to delete the row. I don't know what I am doing wrong. How can I delete my tableview row? Can someone please help me? Thanks in advance.

You should remove the cell at the indexPath that is passed in editActionsForRowAt like this:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let remove = UITableViewRowAction(style: .default, title: " ") { action, indexPath in
tableView.beginUpdates()
tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .left)
tableView.endUpdates()
}
remove.backgroundColor = UIColor(patternImage: UIImage(named: "delete")!)
return [remove]
}
It will delete the row but it won't be consistent with the data inside the array. You can also delete item from the array at the specific index and then call tableView.reloadData()

Once you delete row from tableview you need to reload rows
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let remove = UITableViewRowAction(style: .default, title: " ") { action, indexPath in
tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .left)
self.tableView.reloadRows(at: [IndexPath], with: .fade)
}
remove.backgroundColor = UIColor(patternImage: UIImage(named: "delete")!)
return [remove]
}

Related

Disable delete option for specific UITableViewCell in a UITableView

for UITableViews I have added three difference cells
How to Enable delete option for specific cell
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
switch dataSource[indexpath.section].menu {
case "Attachment":
if editingStyle == AttachmentTableViewCell.EditingStyle.delete {
attachmentList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
}
break
default:
break
}
}
In this case it's shows Delete option for other cell too.
How to stop showing the Delete option on swipe to other UITableViewCell.
Implement tableView(_:editingStyleForRowAt:) and return none for the index paths which should not show the delete option.
This worked for me.
Overriding this func from UITableViewController
or implementing it UITableViewDelegate
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
The indexpaths were you want an action return a UISwipeActionsConfiguration else return nil.
like this
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
// return nil on specific rows
guard indexPath.row != 1 else {
return nil
}
let contexualAction = UIContextualAction(style: .normal, title: "Action") { _, _, _ in
// Do Action
}
let swipeAction = UISwipeActionsConfiguration(actions: [contexualAction])
return swipeAction
}

How to delete from tableview as well as sqlite3 using Swift

I'm new to swift and sqlite3 and I need help on how to delete from tableview and sql db.
I tried to use reloadData() but it doesn't work. I tried to delete using tableView.deleteRows(at: [indexPath], with: .fade) but Im getting an error as I have a sql delete statement running before that. With this code provided below, Im successfully able to remove the item from the database, but it doesn't refresh the tableview. The way I got around to fixing it temporarily is perform a segue to previous screen upon successful removal of an item and when returned to the tableviewcontroller it would be removed.
import UIKit
class TableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
let mainDelegate = UIApplication.shared.delegate as! AppDelegate
#IBOutlet var tableView: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? SiteCell ?? SiteCell(style: .default, reuseIdentifier: "cell")
let rowNum = indexPath.row
tableCell.primaryLabel.text = mainDelegate.people[rowNum].name
tableCell.secondaryLabel.text = mainDelegate.people[rowNum].email
tableCell.myImageView.image = UIImage(named: mainDelegate.people[rowNum].avatar!)
tableCell.accessoryType = .disclosureIndicator
return tableCell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainDelegate.people.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let rowNum = indexPath.row
let details : String! = "Address: \(mainDelegate.people[rowNum].address!) \nPhone Num: \(mainDelegate.people[rowNum].phonenum!) \nEmail: \(mainDelegate.people[rowNum].email!) \nAge: \(mainDelegate.people[rowNum].age!) \nGender: \(mainDelegate.people[rowNum].gender!) \nDate of birth: \(mainDelegate.people[rowNum].dob!)"
let alertController = UIAlertController(title: mainDelegate.people[rowNum].name, message: details, preferredStyle: .alert
)
let cancelAction = UIAlertAction(title: "ok", style: .cancel, handler: nil)
print("TESTING ROW: \(mainDelegate.people[rowNum].id!)")
alertController.addAction(cancelAction)
present(alertController, animated: true)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
var rowNum: Int = indexPath.row
if editingStyle == .delete {
print("Testing delete \(mainDelegate.people[rowNum].id!)")
print("\(indexPath.row)")
mainDelegate.removeFromDatabase(id: mainDelegate.people[rowNum].id!)
print("\(indexPath)")
// tableView.deleteRows(at: [indexPath], with: .fade)
DispatchQueue.main.async{
self.tableView.reloadData()
}
// self.performSegue(withIdentifier: "DataToInfo", sender: self)
// let mainDelegate = UIApplication.shared.delegate as! AppDelegate
// mainDelegate.removeFromDatabase(person: mainDelegate.people[indexPath.row])
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mainDelegate.readDataFromDatabase()
}
removeFromDatabase method
func removeFromDatabase(id : Int){
var db : OpaquePointer? = nil
if sqlite3_open(self.databasePath, &db) == SQLITE_OK{
print("Successfully opened connection to database at \(self.databasePath)")
var deleteStatement : OpaquePointer? = nil
let deleteStatementString : String = "delete from entries where id=\(id)"
if sqlite3_prepare_v2(db, deleteStatementString, -1, &deleteStatement, nil) == SQLITE_OK{
if sqlite3_step(deleteStatement) == SQLITE_DONE{
print("Deleted")
}
else{
print("Failed")
}
}else{
print("Couldn't prepare")
}
sqlite3_finalize(deleteStatement)
sqlite3_close(db)
}
}
Im trying to delete it from tableview as well as database. At one point I was trying to
mainDelegate.people.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
then running the removeFromDatabase, but it was giving me an error.
You should update your datasource. Try to refactor your commitEditing like this:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
var rowNum: Int = indexPath.row
if editingStyle == .delete {
print("Testing delete \(mainDelegate.people[rowNum].id!)")
print("\(indexPath.row)")
mainDelegate.removeFromDatabase(id: mainDelegate.people[rowNum].id!)
print("\(indexPath)")
mainDelegate.readDataFromDatabase()
tableView.deleteRows(at: [indexPath], with: .fade)
}
}

I want to delete a row on tableview , but I'm receiving an error?

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskCell
if editingStyle == .delete {
task.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: cell, with: .automatic)
tableView.endUpdates()
}
}
the error is
Cannot convert value of type 'TaskCell' to expected argument type '[IndexPath]'
You are passing cell instead of indexPath to the deleteRows function.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == .delete {
task.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
You need to pass [IndexPath] in place of cell in your code.
Replace below in your code with
tableView.deleteRows(at: cell, with: .automatic)
with
tableView.deleteRows(at: [indexPath], with: .automatic)
First of all you can add this extension for tableview and than delete row than after you need to reload the tableview data like below,
extension UITableView {
func reloadDataAfterDelay(delayTime: TimeInterval = 0.4) -> Void {
self.perform(#selector(self.reloadData), with: nil, afterDelay: delayTime)
}
}
the tableview rowaction look like this.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let repo = numberofrecord[indexPath.row]as! [String] // your datasource
let delete = UITableViewRowAction(style: .destructive, title: "Delete", handler: {
act,del in
let query = "delete from repost where id=\(repo[0])"; // delete the datasource from databases.
let s = db.dml(query: query)
if s == true {
print("media delete")
}
else{
print("not deleted");
}
self.numberofrecord.remove(at: indexPath.row) // delete datasource from array.
self.tbl.beginUpdates()
self.tbl.deleteRows(at: [indexPath], with: .automatic) // delete row of table.
self.tbl.endUpdates()
self.tbl.reloadDataAfterDelay() // Reload the table after some delay.
})
return [delete]
}
this is the proper and perfectly work in swift.

Adding swipe button in table view in swift

I have a simple table view showing a list of tasks. I want to show two buttons when user swipes on a cell. A delete button to delete the cell and Completed button to store the task in completed array. I am able to implement the delete button but no idea of showing a second button in the table cell. here is the code.
import UIKit
var taskArray = [String]()
var datesArray = [String]()
class ViewController: UIViewController, UITableViewDataSource
{
#IBOutlet weak var taskTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return taskArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row + 1). \(taskArray[indexPath.row])"
cell.detailTextLabel?.text = datesArray[indexPath.row]
return cell
}
override func viewDidLoad()
{
super.viewDidLoad()
taskTableView.dataSource = self
let userDefaults = UserDefaults.standard
if let task = userDefaults.stringArray(forKey: "tasks") , let date = userDefaults.stringArray(forKey: "dates")
{
taskArray = task
datesArray = date
}
print(taskArray)
print(datesArray)
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
taskTableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// this method handles row deletion
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == .delete
{
// remove the item from the data model
taskArray.remove(at: indexPath.row)
datesArray.remove(at: indexPath.row)
// delete the table view row
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
//function to come back from close button
#IBAction func close(segue: UIStoryboardSegue)
{
}
}
Swift 4.0
You can write below method of tableView to define custom swipe action.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
}
delete.backgroundColor = UIColor.red
let complete = UITableViewRowAction(style: .default, title: "Completed") { (action, indexPath) in
// Do you complete operation
}
complete.backgroundColor = UIColor.blue
return [delete, complete]
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
//self.isEditing = false
print("more button tapped")
}
more.backgroundColor = UIColor.lightGray
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
//self.isEditing = false
print("favorite button tapped")
}
favorite.backgroundColor = UIColor.orange
let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
//self.isEditing = false
print("share button tapped")
}
share.backgroundColor = UIColor.blue
return [share, favorite, more]
}
First make this function return true
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
it makes your cell editable , apple provides default deleting and editing options that you can use as like this :
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if(editingStyle == .delete)
{
myArray.remove(at: indexPath.item)
table.deleteRows(at: [indexPath], with: .automatic)
table.reloadData()
}
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if(studentUser as? String == "Admin")
{
return .delete
}
else
{
return .none
}
}
or you can define your custom ones :
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let del = UITableViewRowAction(style: .normal, title: "Delete")
{
(action, index) in
let alert = FCAlertView()
alert.makeAlertTypeCaution()
alert.cornerRadius = 10
alert.delegate = self
alert.animateAlertInFromBottom = true
alert.animateAlertOutToTop = true
alert.bounceAnimations = true
alert.blurBackground = true
alert.dismissOnOutsideTouch = true
alert.showAlert(inView: self,
withTitle: "Title you want ",
withSubtitle: "Subtitle Comes here",
withCustomImage: nil,
withDoneButtonTitle:"OK" ,
andButtons:["Cancel"])
}
let edit = UITableViewRowAction(style: .default, title: "Edit")
{
(action, index) in
self.view.makeToast("Editing Coming soon...")
}
del.backgroundColor = AppColor.myNewRedColor
edit.backgroundColor = .lightGray
return [edit,del]
}
Swift 4.0
Add Delegate & DataSource
tableView.delegate = self
tableView.dataSource = self
Add DataSource func "canEditRowAt indexPath"
//MARK: - UITableViewDataSource
public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
Add Delegate func "editActionsForRowAt indexPath"
//MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in
//Action edit
print("Action Edit...")
})
editAction.backgroundColor = UIColor.gray //Set button color
let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, indexPath) in
//Action delete
print("Action Delete...")
})
return [deleteAction, editAction]
}
I hope this helps.
As par your Requirement i have . created Demo for you.
Here is the Output,
When you press Delete element will be removed from Array and when you press Add Button element will be added to new Array.
Here is the link of Demo,
Tableview Demo with Swipable Add and Delete
Step 1:- Connect your Tableview datasource and delegate in Storyboard.
Step 2:- Write DataSource Methods of TableView.
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Preloaded Data"
} else {
return "Added Data to New Array"
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return arrPrelodedData.count
} else {
return arrAddNewData.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeToDelete", for: indexPath) as? SwipeToDelete else {return UITableViewCell()}
if indexPath.section == 0{
cell.lblCellContent.text = arrPrelodedData[indexPath.row] }
else {
cell.lblCellContent.text = arrAddNewData[indexPath.row]
}
return cell
}
//With this we can edit UITableview ex. Swipe to Delete
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 0 {
return true } else {
return false
}
}
//Select tableview Editing Style (insert and Delete)-> if custom icon than set None
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.none
}
//Delete Action 1) Create delete Action 2) Remove data with Indexpath 3) fetch data from coredata 4) delete tableview row 4) set delete button background color 5) return deleteAction in arry wether it is single
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
//Destructive Because we want to delete(destroy) the data from tableview
let deleteAction = UITableViewRowAction(style: .destructive, title: "DELETE") { (rowAction, indexpath) in
self.arrPrelodedData.remove(at: indexPath.row)
tableView.deleteRows(at: [indexpath], with: .automatic)
}
let addAction = UITableViewRowAction(style: .normal, title: "ADD 1") { (rowAction, indexpath) in
self.arrAddNewData.append(self.arrPrelodedData[indexPath.row])
tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)
// tableView.reloadRows(at: [indexPath], with: .automatic)
}
deleteAction.backgroundColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
addAction.backgroundColor = #colorLiteral(red: 0.9176470588, green: 0.662745098, blue: 0.2666666667, alpha: 1)
return [deleteAction,addAction]
}
}
I hope this answer will helpful for you.

Add and delete cell from table view in swift

I have a table view in the cell i'm having two text fields in which user can enter the data. Initially i'm showing 5 cells. There is a button on which when user click it add one more cell in the table view. Now when i hit a button it add a cell when textfields are empty. But when i add data in all 5 cell textfields and than hit add button app crashes by showing this error, Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 10 into section 0, but there are only 6 rows in section 0 after the update'
The code is try for adding and deleting cell is this,
extension FlashCardViewController: UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfCell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = flashCardTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FlashCardTableViewCell
//cell.termTxt.delegate = self
//allCellsText[indexPath.row] = cell.termTxt.text!
// cell.definitionTxt.delegate = self
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 115
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
numberOfCell -= 1
allCellsText.remove(at: indexPath.row)
flashCardTableView.beginUpdates()
flashCardTableView.deleteRows(at: [indexPath], with: .automatic)
flashCardTableView.endUpdates()
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
allCellsText.append(textField.text!)
print(allCellsText)
}
}
The code for add button is this,
#IBAction func addCardBtnTapped(_ sender: Any) {
numberOfCell += 1
let indexPath = IndexPath(row: allCellsText.count+1, section: 0)
flashCardTableView.beginUpdates()
flashCardTableView.insertRows(at: [indexPath], with: .automatic)
flashCardTableView.endUpdates()
view.endEditing(true)
}
When i delete any cell it gives me error of index out of range. How can i achieve this goal? The view controllers looks like this,
The problem is in the way you create an indexPath for inserting a new row, fix it according to this:
#IBAction func addCardBtnTapped(_ sender: Any) {
numberOfCell += 1
// create indexPath from numberOfCell, not from allCellsText.count
let indexPath = IndexPath(row: numberOfCell - 1, section: 0)
flashCardTableView.beginUpdates()
flashCardTableView.insertRows(at: [indexPath], with: .automatic)
flashCardTableView.endUpdates()
view.endEditing(true)
}
The problem is in creating the IndexPath using IndexPath(row: allCellsText.count+1, section: 0). The insertions and deletions on tableView HAVE to be consistent with the dataSource - if you add a new row, the numberOfRowsInSection HAVE to increase by one, too. Now in your case you increment numberOfCell by one, as you are supposed to do, but then you try to add the new row at an indexPath determined by allCellsText.count+1. The problem is that the allCellsText.count is not consistent with numberOfCell variable (notice that you append a new string everytime textFieldDidEndEditing gets called).
EDIT
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allCellsTermText.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = flashCardTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FlashCardTableViewCell
// configure it with the backing data
cell.termTxt.text = allCellsTermText[indexPath.row]
cell.definitionTxt.text = allCellsDefinitionText[indexPath.row]
// now instead of this you will have to find a way how you will be
// able to determine the row which needs to be changed and change the model
// cell.termTxt.delegate = self
// cell.definitionTxt.delegate = self
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 115
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
allCellsTermText.remove(at: indexPath.row)
allCellsDefinitionText.remove(at: indexPath.row)
flashCardTableView.deleteRows(at: [indexPath], with: .automatic)
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
// you should not append here, this will add a new row, you have to UPDATE the proper text
// allCellsText.append(textField.text!)
}
#IBAction func addCardBtnTapped(_ sender: Any) {
// create a new row by appending new empty strings
allCellsTermText.append("")
allCellsDefinitionText.append("")
let indexPath = IndexPath(row: allCellsTermText.count - 1, section: 0)
flashCardTableView.insertRows(at: [indexPath], with: .automatic)
view.endEditing(true)
}
If you want to use button to delete tableviewCell, any table view that allows rows to be deleted
Image TableViewCell delete with button
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ATableViewCell
cell.deleteButton.addTarget(self, action: #selector(nHapusTap(_:)), for: .touchUpInside)
return cell
}
#objc func nHapusTap(_ sender: UIButton) {
let hitPoint = sender.convert(CGPoint.zero, to: tableView)
if let indexPath = tableView.indexPathForRow(at: hitPoint) {
self.dataArray.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
You're doing it incorrect. You've added self as delegate to the termtxt and definitiontxt here.
cell.termTxt.delegate = self
cell.definitionTxt.delegate = self
As many times as you end editing in these inputboxes, your delegate method gonna get hit where you're just appending the text in a array.
func textFieldDidEndEditing(_ textField: UITextField) {
allCellsText.append(textField.text!)
print(allCellsText)
}
When you call the add row button, the allCellsText size is 10 because endEditing has been called from two types of inputboxes. (10 is incorrect, you can't add 10th row after 5th row).
let indexPath = IndexPath(row: allCellsText.count+1, section: 0)
Solution:
Either add some checks in your delegate method before appending anything in allCellsText array OR update your logic in addCardBtnTapped function, remove the dependency from allCellsText arrow.
use something like:
let indexPath = IndexPath(row: numberOfCell+1, section: 0)

Resources