How reload tableView after searching and deleting a data from sqlite - swift

I added a button on an UITableView to delete data in SQLite. The button works perfectly but I'm having a hard time reloading the tableview after deletion.
I used tableview.reload() but it wouldn't reload while I'm searching. If I add two data such as "aaa" and "aaaa" and search "aa" to delete one, you can see the two data and if you delete "aaaa", it doesn't reload automatically, and if you remove "a" after that deletion, which means the left letters are "aaa". It shows up. without typing again, there would be another way to reload tableview automatically after deletion?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomizedCell
cell.requiredAction = { [unowned self] in
let DB = FMDatabase(path: self.databasesPath)
if DB.open() {
let result : FMResultSet? = DB.executeQuery("select * from table", withArgumentsIn: [])
self.fullData = []
while result?.next() == true {
if let appendedData = result?.string(forColumn: "keyword") {
self.fullData.append(appendedData)
}
}
self.targetData = self.searching ? self.searchedArr : self.fullData
let query = "delete from table where keyword='\(self.targetData[indexPath.row])'"
DB.executeStatements(query)
self.fullData = []
let secondResult: FMResultSet? = DB.executeQuery("select * from table", withArgumentsIn: [])
while secondResult?.next() == true {
if let appendedData = secondResult?.string(forColumn: "keyword") {
self.fullData.append(appendedData)
}
}
self.tableview.reloadData()
self.targetData = self.searching ? self.searchedArr : self.fullData
}
}
cell.myLableCell1.font = UIFont.boldSystemFont(ofSize: 17)
targetData = searching ? searchedArr : fullData
cell.myLableCell1?.text = targetData[indexPath.row]
return cell
}

Hi you are loading data in tableview cell file....just do the out of the class....and after
create one getdata method in class and fill array
then delete row and data from sqlite...
after again call getdata.
In getdata method you need to write tableview.reload()
i hope you got....
follow instagram : styoloholic_007 and gunatitsolutions

Related

core data fetching int not displaying on tableview cell

I am writing swift code with the goal of displaying a increasing number on every tableview cell. Right now the int is not being display. So the first tableview cell should say 1 and the 2nd should say 2. You can see in the gif below what is going along with the tableview cell and nothing is appearing in them when the button is clicked. The func below is when the button is clicked.
var pageNumber = 1
var itemName : [Player] = []
func enterData() {
theScores.reloadData()
let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
let context = appDeldeaget.persistentContainer.viewContext
// Simpler way to create a new Core Data object
let theTitle = Player(context: context)
// Simpler way to set the position attribute
theTitle.positon = Int64(pageNumber)
print(pageNumber)
// pageNumber must be of type Int64, otherwise use Int64(pageNumber)
do {
try context.save()
itemName.append(theTitle)
pageNumber += 1
} catch {
// handle errors
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = itemName[indexPath.row]
let cell = theScores.dequeueReusableCell(withIdentifier: "MyCell", for : indexPath)
cell.selectionStyle = .default
let attr5 = title.value(forKey: "positon") as? String
let text = [" Item :", attr5].compactMap { $0 }.reduce("", +)
cell.textLabel?.text = "\(text)"
cell.textLabel?.textAlignment = .center
cell.layoutMargins = UIEdgeInsets.zero
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
return cell
}
Here's why it doesn't work. You have this:
let attr5 = title.value(forKey: "positon") as? String
let text = [" Item :", attr5].compactMap { $0 }.reduce("", +)
This is a really complicated way to try and do this, and it doesn't work as written. The problem is that the value of position is an Int64 and you need a string. But using as? like that doesn't turn it into a string. When that line of code runs, Swift says, can I just make this into a string? But it can't. So the as? String is nil, and your table cells don't include the number because the conversion failed.
A better way would be something like
if let position = title.value(forKey: "positon") {
cell.textLabel?.text = "Item : \(positon))"
}
But that's only if you really want to use value(forKey:) for some reason. You probably don't need that because normally Xcode creates a subclass of NSManagedObject for each entity with named properties. So even better would be
cell.textLabel?.text = "Item: \(title.position)"
These both work because string interpolation knows how to convert an integer to a string.
You probably should call .reloadData() after context.save()

how to remove the cell from uitableview cell

Im trying to dynamically arranging table view when user select "type 3". It works when user select "type 3", "type 3-1" would be added in the tableview. However the program crashed when user select other than type3-1. I dont know how can I execute the "rows.remove(at:2)" before the override function is called. Any suggestion would appreciate!
class GuestViewController: UITableViewController {
var rows:[[[String:Any]]] = [[["type":RowType.DetailContent,
"subType":DCType.DCRightContent,
"name":CPFFields.CID,
"content":"9637"],
["type":RowType.DetailContent,
"subType":DCType.DCInput,
"name":CPFFields.VISIA]],
[["type":RowType.DetailTextView,
"CPFType":CPFFields.UV,
"title":CPFFields.preferenceTitle]],
[["type":RowType.DetailContent,
"subType":DCType.DCSelection,
"name":CPFFields.Phototherapy,
"title":CPFFields.anestheticTitle],
["type":RowType.DetailTextView,
"CPFType":CPFFields.Phototherapy,
"title":CPFFields.preferenceTitle]],
]
var isNewGuestSelected : Bool = false
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = rows[indexPath.section][indexPath.row]
let type = item["type"] as! RowType
if type == RowType.DetailContent
{
let cell = tableView.dequeueReusableCell(withIdentifier: "DetailNameCell", for: indexPath) as! DetailContentCell
let cpfType = item["name"] as? CPFFields ?? .Customer
cell.name.text = CPFFields.localizedString(from: cpfType)
if let field = item["title"] as? CPFFields
{
cell.name.text = CPFFields.localizedString(from: field)
}
cell.moreSlectionLeftSpace = true
var content:String? = ""
cell.type = cpfType
switch cpfType {
case .CID:
content = (profile?.birthDate.dateFromDateString?.stringForPaitentId ?? "") + (profile?.name ?? "")
case .CT:
content = ""
if let profile = profile
{
content = CPFCustomerType.localizedString(from: profile.type)
//New Guest
if(content == CPFCustomerType.type1.rawValue){
rows[0].insert(["type":RowType.DetailContent,
"subType":DCType.DCRightContent,
"name":CPFFields.CID,
"content":"9637"], at: 1)
isNewGuestSelected = true
} else{
if isNewGuestSelected == true{
rows[0].remove(at: 1)
isNewGuestSelected = false
}
}
}
let subType = item["subType"] as! DCType
cell.setcontentType(type: subType, content: content)
return cell
}
I expected not to see "rows[0][2]" after running "rows[0].remove(at:1)".
However the log is printing
rows[0][0]
rows[0][1]
rows[0][2]
then
it crashed at "let item = rows[indexPath.section][indexPath.row]"
because it is out of range
You are modifying your content while rendering, thus after numberOfRows:inSection: was called. Therefore the tableView is trying to access an element that no longer exists, since you removed it.
Your cycle:
→ number of rows 4
→ removed item, contents now has 3 items
→ cell for item 0
→ cell for item 1
→ cell for item 2
- cell for item 3 → crash
Consider replacing the logic you have here outside of the cellForRow method, and doing these operations before you reload your tableView.
You should use the tableView:cellForRow:atIndexPath strictly for dequeueing your cells and configuring them; not for modifying the underlying data store since funky things like you're experiencing now can happen.
If you provide a bit more context I can probably tell you where to place your code to fix this issue.
Actually, the solution is quite simple. I just added tableView.reloadData() after removing the array, and the UI can then be updated.
if isNewGuestSelected == true{
rows[0].remove(at: 1)
isNewGuestSelected = false
tableView.reloadData()
}

How to fetch the image from Firebase and save it to the Image in tableViewCell?

I am using a TableViewController and fetching image from firebase inside a Closure. I do get the name of the images in a local Database and saving it in an array at the time of fetching. But when I access the image array using indexPath.row everything gets collapsed(Instead of 2 images I need, all the images are getting replaced and it keeps on getting increased when I scroll the tableView). Please do guide me how to fetch the image inside the cell. Thanks in advance
var imageList = [UIImage]()
func getData()
{
Data = NSMutableArray()
Data = ModelManager.getInstance().getAllData(planID)
let count = Data.count
for i in 0...count-1{
let demoPlan = (Data.object(at: i) as! workoutPlan)
ImageDemo.append(demoPlan.Image)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell2:workoutplanCell = tableView.dequeueReusableCell(withIdentifier: "workoutplanCell") as! workoutplanCell
cell2.planExerciseNameLabel.text = plan.Name
if(plan.Image.contains("plank")){
cell2.timerButton.isHidden = false
}
else{
cell2.timerButton.isHidden = true
}
var new_imgList = [String]()
new_imgList.append(ImageDemo[indexPath.row])
new_imgList.append(ImageDemo[indexPath.row] + "1")
let storage = Storage.storage().reference()
for x in new_imgList
{
let storageRef = storage.child("images/\(new_imgList[x]).jpg")
storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
if let error = error{
print(error.localizedDescription)
return
}
else{
self.imageList.append(UIImage(data: data!)!)
}
if x == new_imgList{
cell2.planExerciseImage.animationImages = self.imageList
cell2.planExerciseImage.animationDuration = 2
cell2.planExerciseImage.startAnimating()
cell2.planDescription.delegate = self
cell2.planDescription.text = plan.Description
cell2.planDescription.sizeToFit()
cell2.planDescription.textColor = UIColor.black
self.imageList.removeAll()
new_imgList.removeAll()
}
}
}
return cell2
}
You probably have that problem for two reasons:
One is the imageList scope. You have defined it somewhere in your ViewController and everytime a cell is set it keeps adding images.
And the second reason that is somewhat connected to the first one, is the if x == 1 i cant see that statement being called all the time, consequently your imageList will not be cleared for every cell.
Remove your new_imgList from wherever you have declared it and instead declare it here:
for x in new_imgList{
// DECLARE new_imgList HERE
.....
}
and delete the self.imageList.removeAll() line.

display array in a label in swift

I would like two display Object data gotten from Parse in swift. I have tried using label in this way but it only displays the last element in the object. Please how can I make it display all the element in the object in the label. Like one element to one label. Thanks
let query = PFQuery(className: "Questionnaire")
query.findObjectsInBackground { (objects, error) -> Void in
if error == nil {
// There were no errors in the fetch
if let returnedObjects = objects {
// var text = ""
// Objects Array is not nil
// loop through the array to get each object
for object in returnedObjects {
print(object["question"] as! String)
// text.append(object["question"] as! String)
self.Label.text = (object["question"] as! String)
}
}
}
}
You can do in one line like that and join all question with , separator , you can change separator to any (empty, -,...etc)
if let returnedObjects = returnedObjects {
self.Label.text = returnedObjects.map {($0["question"] as? String) ?? nil}.compactMap({$0}).joined(separator: ",")
}
Use tableview for this.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! YouTableViewCell
cell.textLabel.text = yourArray[indexpath.row] as? String ?? ""
return cell
}
If it's important to use UILabel
var concatenatedString = ""
for object in returnedObjects {
concatenatedString += object["question"] as! String
}
self.Label.text = concatenatedString
You are looping through the array and setting each value to Label.text. However, setting Label.text will replace what was on the label before. That's why you only see the last item.
One solution is to display the string representation of the array:
self.Label.text = "\(object)"
Another solution is to display the items in a table view Suganya Marlin has suggested. You would need to conform to UITableViewDatasource and implement the various methods. Here is a guide.

Slow CloudKit table scrolling - altering existing code?

Below I have my existing query download and cell for table row code...
publicDB.perform(query, inZoneWith: nil)
{
(results, error) -> Void in
if (error != nil)
{
self.present(alert, animated: true, completion: nil)
}
else
{
for result in results!
{
self.restaurantArray.append(result)
}
OperationQueue.main.addOperation( { () -> Void in
self.tableView.reloadData()
}) } }}
downloadRestaurants()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell
let restaurant: CKRecord = restaurantArray[indexPath.row]
cell?.name?.text = restaurant.value(forKey: "Name") as? String
let asset = restaurant.value(forKey: "Picture") as! CKAsset
let data = try! Data(contentsOf: asset.fileURL)
_ = UIImage(data: data)
cell?.picture?.image = UIImage(data: data)
return cell!
}
When I run this code, the app remains functional but scrolling through the 10 or so table cells is incredibly choppy. I am unsure what is causing this - all records, each containing an image, are downloaded during the query download portion of the top function. However, a problem or concept I'm missing is ever present during runtime. What am I missing here? Lazy loading? cache? something else? Unsure at this point, so any help would be incredibly helpful.
Update 1:
I've updated my code with a large thank you going to Pierce. I've had to update my code ever so slightly from his answer to maintain a ckrecord array to segue over to another controller via - restaurantArray but also create a new array for the NSObject class - tablerestaurantarray to be displayed in the current table controller.
var restaurantArray: Array<CKRecord> = []
var tablerestaurantarray: [Restaurant] = []
for result in results!
{
let tablerestaurant = Restaurant()
if let name = result.value(forKey: "Name") as! String? {
tablerestaurant.name = name
}
// Do same for image
if let imageAsset = result.object(forKey: "Picture") as! CKAsset? {
if let data = try? Data(contentsOf: imageAsset.fileURL) {
tablerestaurant.image = UIImage(data: data)
}
}
self.tablerestaurantarray.append(tablerestaurant)
self.restaurantArray.append(result)
}
OperationQueue.main.addOperation( { () -> Void in
self.tableView.reloadData()
})
}
}
}
downloadRestaurants()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell
let restaurant: Restaurant = tablerestaurantarray[indexPath.row]
cell?.name?.text = restaurant.name
cell?.picture?.image = restaurant.image
return cell!
}
The way your code is setup, whenever you scroll in your UITableView, your program is converting a CKAsset into Data, and then converting that into a UIImage, and that's within every cell! That's a rather inefficient process, so try creating an NSObject called something like Restaurant that has an image property, and when you go through all the records returned from your CKQuery, parse each record into a new Restaurant object. To create a new NSObject, go to File -> New -> File -> select 'Swift File' and add something like this:
import UIKit
class Restaurant: NSObject {
// Create a UIImage property
var image: UIImage?
// Add any other properties, i.e. name, address, etc.
var name: String = ""
}
Now for your query:
// Create an empty array of Restaurant objects
var restaurantArray: [Restaurant] = []
publicDB.perform(query, inZoneWith: nil) { (results, error) -> Void in
if (error != nil) {
self.present(alert, animated: true, completion: nil)
} else {
for result in results! {
// Create a new instance of Restaurant
let restaurant = Restaurant()
// Use optional binding to check if value exists
if let name = result.value(forKey: "Name") as! String? {
restaurant.name = name
}
// Do same for image
if let imageAsset = result.object(forKey: "Picture") as! CKAsset? {
if let data = try? Data(contentsOf: imageAsset.fileURL) {
restaurant.image = UIImage(data: data)
}
}
// Append the new Restaurant to the Restaurants array (which is now an array of Restaurant objects, NOT CKRecords)
self.restaurantArray.append(restaurant)
}
OperationQueue.main.addOperation( { () -> Void in
self.tableView.reloadData()
})
}
}
Now your cell setup is much simpler:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell
let restaurant: Restaurant = restaurantArray[indexPath.row]
cell?.name?.text = restaurant.name
cell?.picture?.image = restaurant.image
return cell!
}
You should use CKQueryOperation in order to implements pagination for your UITableView.
You have to set the resultLimit property to a number equals to the cell quantity visiable at one time on you table plus 3 or 4
Set recordFetchedBlock property where you have to implement the code that will apply to one CKRecord
Set queryCompletionBlock property. This is the most important part on your pagination code because this closure receive an Optional CKQueryCursor parameter.
If this CKQueryCursor is nil then you have reach the last record available for you query but if it's a non nil value, then you have more records to fetch using this CKQueryCursor as indicator to your next fetch.
When user scroll on your TableView and reach the last element you should perform another fetch with CKQueryCursor.
Other performance advice is CKAssets should be treated on separated execution queues.