How to permanently delete a row from table view in Swift? - ios

I tried to delete the rows by using the delete code but the row reappear everytime. I want to permanently delete any particular row.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var countries = ["India","Canada", "USA","Russia","Dubai"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countries.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = countries[indexPath.row]
return cell
}
//to enable delete action by swiping left
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
countries.remove(at: indexPath.row)
tableView.reloadData()
}
}
#IBOutlet weak var userTxt: UITextField!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
let obj = UserDefaults.standard.object(forKey: "userKey") as? String
if let userName = obj {
userTxt.text = userName
}
}
#IBAction func savePressed(_ sender: UIButton) {
//we use this to save data
UserDefaults.standard.set(userTxt.text, forKey: "userKey")
}
#IBAction func deletePressed(_ sender: UIButton) {
UserDefaults.standard.removeObject(forKey: "userKey")
}
}

You have to save the modified array of countries to persist the changes you have made to it. Here's what you need to do:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//...
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
countries.remove(at: indexPath.row)
UserDefaults.standard.set(countries, forKey: "countries")
tableView.reloadData()
}
}
//...
override func viewDidLoad() {
super.viewDidLoad()
if let array = UserDefaults.standard.array(forKey: "countries") as? [String] {
countries = array
} else {
countries = ["India","Canada", "USA","Russia","Dubai"]
}
//...
}
}

Your implementation of delete does not persist the changes you make from delete as it uses a variable that’s initialized every time with data you hardcoded, hence array restores its elements to ones you specified every time you check back. So instead of hard coding values to an array to fill your tableView, consider using a persistent storage like a database or UserDefaults.

Related

How to show info on table view

Im trying to load a array of strings in a table view but the view does not recognize the array.
I have an array called Playlists (declared as global on ThirdViewController) with objects from class Playlist. When I use it on every other table view I can access every object and use it on the table view (I'm using it on ThirdViewController), but on AddToPlaylist view I can't use it. I think I'm using correctly the cells and func for table views.
This happens when I press the button "Añadir" on player view. It should load the table view with the array info.
Here is the project (develop branch): tree/develop
import UIKit
class AddToPlaylist: UIViewController, UITableViewDelegate, UITableViewDataSource{
#IBOutlet weak var myTableViewPlaylist: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Playlists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "hola", for: indexPath)
cell.textLabel?.text = Playlists[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Playlists[indexPath.row].songs.append(songName)
performSegue(withIdentifier: "addedSong", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
myTableViewPlaylist.delegate = self
myTableViewPlaylist.dataSource = self
myTableViewPlaylist.reloadData()
}
}
Here is the declaration of Playlists array:
import UIKit
import AVFoundation
var favorites:[String] = []
var Playlists:[Playlist] = []
var selecPlaylist = 0
var firstOpen2 = true
class ThirdViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var myTableView2: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//print(Playlists.count)
return Playlists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = Playlists[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selecPlaylist = indexPath.row
performSegue(withIdentifier: "segue", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
myTableView2.delegate = self
myTableView2.dataSource = self
if firstOpen2{
crear()
firstOpen2 = false
}
myTableView2.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func crear(){
let pl1 = Playlist(name: "Prueba")
pl1?.addSong(song: songs[0])
Playlists.append(pl1!)
let pl2 = Playlist(name: "Prueba2")
pl2?.addSong(song: songs[1])
Playlists.append(pl2!)
}
}
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! UITableViewCell
cell.textLabel?.text = Playlists[indexPath.row].name
return cell

How to delete custom cell with timer in UITableView?

I'm developing an application that has a "Plus" button which can add stopwatch to a table view, every cell has its own timer, and can be played by itself.
When I'm trying to delete one cell like that, random issues are happening like:
Order of the stopwatches being changed
some stopwatches time is being zeroed .
If trying to add new stopwatch after, an old stopwatch with it's timer are back!
TableView
class StopWatchViewController: UIViewController {
#IBOutlet weak var stopWatchesTableView: UITableView!
var stopwatchesList: [String] = []
var stopwatchesNum : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
stopWatchesTableView.delegate = self
stopWatchesTableView.dataSource = self
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidEnterBackground(noti:)),
name: UIApplication.didEnterBackgroundNotification,
object: nil)
}
#objc func applicationDidEnterBackground(noti: Notification) {
// Save Date
let shared = UserDefaults.standard
shared.set(Date(), forKey: "SavedTime")
print(Date())
}
func refresh() {
stopWatchesTableView.reloadData()
}
#IBAction func AddStopWatch(_ sender: Any) {
stopwatchesNum += 1;
stopwatchesList.append(String(format: "Stopwatch %d", stopwatchesNum))
refresh()
}
}
extension StopWatchViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stopwatchesList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let stopWatch = stopwatchesList[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "StopwatchCell") as! StopWatchCell
cell.initCell(title: stopWatch, index: indexPath.row)
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
stopwatchesList.remove(at: indexPath.row)
stopWatchesTableView.deleteRows(at: [indexPath], with: .automatic)
refresh()
}
}
}
What can cause such issues ?
Don't call refresh method after deleting the row.Hope this help.

tableview does not update

i'm trying to make a ordering food app for restaurants, when i add items to the cart for the first time table view loads the singleton array i've made. but when i go back to the menu and choose another item the array is updated but the table view of the cart doesn't, i'm using tabbarcontroller. tried to use tableview.reloadData() in different places still new data added to the array doesn't appear
class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
#IBOutlet weak var priceLbl: UILabel!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
updateView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.cartItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as? CartCell{
let item = DataService.instance.cartItems[indexPath.row]
cell.configCell(cart: item)
return cell
} else {
return UITableViewCell()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
DataService.instance.cartItems.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
updateView()
tableView.endUpdates()
}
}
func updateView(){
var sum = 0
for item in DataService.instance.cartItems{
sum += item.itemPrice * item.quantity
print(item.itemPrice)
}
priceLbl.text = "$\(sum)"
}
#IBAction func orderPressed(_ sender: Any) {
// upload order to firebase, clear cart and move to orderVC
}
}
In tabbar every time viewDidLoad not called so you need to reload the data in viewWillAppear or viewDidAppear. Here is the reference.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
updateView()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
UITableViewDelegate is not reactive. You have to call reloadData() when you want to update the tableView content (when your DataService.instance value is updated).
In a MVP world, your "add" action will trigger an event to the presenter. And the presenter sends back to the view an action to update the tableView, after updating your DataService.
Maybe you should look at RxSwift/ReactiveCocoa, which provides APIs to automatically bind your DataService.instance array to the cells rendered in the UITableView.

Pass textfield to a label in a second view controller in swift

I am making a simple client invoice list using a tableView with custom labels in each row. at the top you click the plus sign. This takes you to a second page where you type in your clients name. you hit save and it should add it to the label in the first view controller. I cannot get this to work.
my tableview storyboard has the file called ViewController.swift - in it
import UIKit
var invoiceList = [""]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var clientTableView: UITableView!
#IBOutlet var clientLabel: UILabel!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return invoiceList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "clientItem", for: indexPath) as! UITableViewCell
let ClientName = invoiceList[indexPath.row]
cell.textLabel?.text = [clientInput]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete
{
invoiceList.remove(at: indexPath.row)
clientTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
clientTableView.reloadData()
}
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.
}
}
My second storyboard where you type in the text field. File is called AddInvoiceItem.swift. in it -
import UIKit
class AddInvoiceViewController: UIViewController {
var clientTextField:String!
#IBOutlet var clientInput: UITextField!
#IBAction func addItem(_ sender: Any) {
if clientInput.text != ""
{
invoiceList.append(clientInput.text!)
clientInput.text = ""
_ = navigationController?.popViewController(animated: true)
}
}
#IBAction func cancelBtn(_ sender: Any) {
_ = navigationController?.popViewController(animated: true)
}
}
My error keeps happening when I am trying to use the code line
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "clientItem", for: indexPath) as! UITableViewCell
let ClientName = invoiceList[indexPath.row]
cell.textLabel?.text = [clientInput]
return cell
}
The cell.textLabel?.text = [clientInput]
I know this is wrong, but cannot figure it out!
Make a new cell class
class CustomCell: UITableViewCell {
//Make your outlets here, connect the outlets from cell in your storyboard like ClientNameLabel
}
Please select the cell on your Storyboard, and enter the name of your class in this field. In your case it would be CustomCell
then in ViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "clientItem", for: indexPath) as! CustomCell
let ClientName = invoiceList[indexPath.row]
cell.ClientNameLabel.text = ClientName
return cell
}

Can't show the core data in tableview Swift

class showPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
#IBOutlet weak var tableView: UITableView!
var records : [Record] = []
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 {
let cell = UITableViewCell()
return cell
}
func tableView(_ tableView: 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")
}
}
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.
}
}
Hello everyone, I just tried to show the core data in table view, I already connect the dataSource and delegate to the ViewController, and I confirmed There are some data in core data, anyone can help me plz? thanks
Two big mistakes:
You cannot create a cell with the default initializer UITableViewCell() you have to dequeue it.
You have to get the item in the data source array for the index path and assign a value of a property to a label of the cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let record = records[indexPath.row]
cell.textLabel!.text = record.<nameOfProperty>
return cell
}
cell is the identifier specified in Interface Builder.
<nameOfProperty> is a property in your data model.

Resources