for loop in SwiftyJSON Response - ios

I have a JSON response from Alamofire. I want populate my tableviews rolls with the JSON but only one roll is displayed. other JSONs are not loaded into to custom tableView cells
If segmentedControl.selectedSegmentIndex == 0 {
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
cell.currentRideDriverPassengerName.text = currentDriverRidePassaengerDataModel.name
cell.currentRideDriverPassengerLocation.text = currentDriverRidePassaengerDataModel.location
cell.currentRideDriverPassengerDestination.text = currentDriverRidePassaengerDataModel.destination
cell.currentRideDriverPassengerPrice.text = String(currentDriverRidePassaengerDataModel.price)
cell.currentRideDriverPassengerSeat.text = String(currentDriverRidePassaengerDataModel.seat)
cell.currentRideDriverPassengerDistance.text = String(currentDriverRidePassaengerDataModel.distance)
cell.currentRideDriverPassengerImage.sd_setImage(with: URL(string: currentDriverRidePassaengerDataModel.image))
}else if segmentedControl.selectedSegmentIndex == 1 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
}
return cell
}
}
func getCurrentRideData(url: String) {
Alamofire.request(url, method: .get).responseJSON {
response in
if response.result.isSuccess {
print("Sucess Got the Current Ride Data")
let currentRideJSON : JSON = JSON(response.result.value!)
let currentDriverPassenger : JSON = JSON(response.result.value!)
print(currentRideJSON)
print("This is passager\(currentDriverPassenger)")
self.updateCurrentRideData(json: currentRideJSON)
self.uodateCurrentRideDriverPassangerData(json: currentDriverPassenger)
}else {
print("error)")
}
}
}
func uodateCurrentRideDriverPassangerData(json : JSON) {
currentDriverRidePassaengerDataModel.image = json["ride"][0] ["riders"][0]["image"].stringValue
currentDriverRidePassaengerDataModel.name = json["ride"][0]["riders"][0]["name"].stringValue
currentDriverRidePassaengerDataModel.location = json["ride"][0]["riders"][0]["startLocation"].stringValue
currentDriverRidePassaengerDataModel.destination = json["ride"][0]["riders"][0]["stopLocation"].stringValue
currentDriverRidePassaengerDataModel.price = json["ride"][0]["rider"][0]["price"].intValue
currentDriverRidePassaengerDataModel.seat = json["ride"][0]["riders"][0]["seatNumber"].intValue
currentDriverRidePassaengerDataModel.distance = json["ride"][0]["riders"][0]["distance"].intValue
}
I want my JSON to populate my table rolls not just one roll

Try This
func uodateCurrentRideDriverPassangerData(json : JSON)
{
for i in 0...json.count-1
{
currentDriverRidePassaengerDataModel.image = json["ride"][i] ["riders"][i]["image"].stringValue
currentDriverRidePassaengerDataModel.name = json["ride"][i]["riders"][i]["name"].stringValue
currentDriverRidePassaengerDataModel.location = json["ride"][i]["riders"][i]["startLocation"].stringValue
currentDriverRidePassaengerDataModel.destination = json["ride"][i]["riders"][i]["stopLocation"].stringValue
currentDriverRidePassaengerDataModel.price = json["ride"][i]["rider"][i]["price"].intValue
currentDriverRidePassaengerDataModel.seat = json["ride"][i]["riders"][i]["seatNumber"].intValue
currentDriverRidePassaengerDataModel.distance = json["ride"][i]["riders"][i]["distance"].intValue
}
}

Related

how to reduce the code in tableView cell in swift

hey i'm in the process of learning to code. I created an app that downloads JSON data - covid.
It looks like this :
enter image description here
my code in function (code below) has become terribly large.
how can I reduce this code?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Cells.covidCell, for: indexPath) as! CovidCell
if inSearchMode == true {
cell.countryLabel.text = filterCovidData[indexPath.row].country
cell.regionLabele.text = filterCovidData[indexPath.row].continent
cell.casesLabel.text = "Case: \(filterCovidData[indexPath.row].cases!)"
cell.deathLabel.text = "Death: \(filterCovidData[indexPath.row].deaths!)"
cell.activelabel.text = "Active: \(filterCovidData[indexPath.row].active!)"
cell.testsLabel.text = "Test: \(filterCovidData[indexPath.row].tests!)"
cell.todayCasesInfo.text = "\(filterCovidData[indexPath.row].todayCases!)"
let imageUrl = filterCovidData[indexPath.row].countryInfo?.flag
fetchImage(withUrlString: imageUrl!) { (image) in
DispatchQueue.main.async {
cell.countryFlag.image = image
}
}
} else {
cell.countryLabel.text = covidData[indexPath.row].country
cell.regionLabele.text = covidData[indexPath.row].continent
cell.casesLabel.text = "Case: \(covidData[indexPath.row].cases!)"
cell.deathLabel.text = "Death: \(covidData[indexPath.row].deaths!)"
cell.activelabel.text = "Active: \(covidData[indexPath.row].active!)"
cell.testsLabel.text = "Test: \(covidData[indexPath.row].tests!)"
cell.todayCasesInfo.text = "\(covidData[indexPath.row].todayCases!)"
let imageUrl = covidData[indexPath.row].countryInfo?.flag
fetchImage(withUrlString: imageUrl!) { (image) in
DispatchQueue.main.async {
cell.countryFlag.image = image
}
}
}
return cell
}
See the repetitive code.
You do the same, except the source of the populate, so, let's just retrieve the model according to your needs (inSearchMode), and then let's call the same code.
let model = inSearchMode ? filterCovidData[indexPath.row] : covidData[indexPath.row]
cell.countryLabel.text = model.country
cell.regionLabele.text = model.continent
cell.casesLabel.text = "Case: \(model.cases!)"
cell.deathLabel.text = "Death: \(model.deaths!)"
cell.activelabel.text = "Active: \(model.active!)"
cell.testsLabel.text = "Test: \(model.tests!)"
cell.todayCasesInfo.text = "\(model.todayCases!)"
let imageUrl = model.countryInfo?.flag
fetchImage(withUrlString: imageUrl!) { (image) in //I'duse a [weak self] here
DispatchQueue.main.async {
cell.countryFlag.image = image
}
}
That's be the first step.
You can have another logic at start:
let arrayToUse = inSearchMode ? filterCovidData : covidData
let model = arrayToUse[indexPath.row]
You can also add a code in CovidCell
func update(model: ModelThatsInsideCovidData) {
countryLabel.text = model.country
regionLabele.text = model.continent
casesLabel.text = "Case: \(model.cases!)"
deathLabel.text = "Death: \(model.deaths!)"
activelabel.text = "Active: \(model.active!)"
testsLabel.text = "Test: \(model.tests!)"
todayCasesInfo.text = "\(model.todayCases!)"
let imageUrl = model.countryInfo?.flag
//Here cell doesn't have that method, should it be accessible?, I'll let you decide.
fetchImage(withUrlString: imageUrl!) { (image) in
DispatchQueue.main.async {
self.countryFlag.image = image
}
}
And then, in cellForRowAt:
let model = ...
cell.update(model: model)
return cell

Dequeue Reusable Cell crashes when calling dequeued cell

I am attempting to have a table view that lists multiple things and allows a user to go through and select multiple cells with checkboxes. My code works up until a certain point, the problem is that the app crashes with the following error
Fatal error: Unexpectedly found nil while unwrapping an Optional value
whenever I call the following code
swift let currentCell = recommendToFriendTableView.cellForRow(at: selectedRow[i]) as? RecommendToFriendsTableViewCell
Here is the method where we set up the cells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (tableView == self.friendListTableView) {
let cell: FriendListTableViewCell = tableView.dequeueReusableCell(withIdentifier: "FriendListCell") as! FriendListTableViewCell
let rowNumber = (indexPath as NSIndexPath).row
var name = ""
if searchActive {
name = filtered[rowNumber]
}
else {
name = names[rowNumber]
}
cell.friendNameLabel.text = name
cell.friendNameLabel.backgroundColor = tableViewBgColor
cell.friendNameLabel.textColor = textColor
cell.recommendToFriendButton.layer.borderWidth = 1
cell.recommendToFriendButton.layer.borderColor = tableViewBgColor.cgColor
cell.recommendToFriendButton.layer.cornerRadius = 6
cell.recommendToFriendButton.backgroundColor = buttonBgColor
cell.backgroundColor = tableViewBgColor
//set target for buttons
cell.recommendToFriendButton.tag = rowNumber
cell.recommendToFriendButton.addTarget(self, action:#selector(recommendToFriendButtonClicked), for: UIControl.Event.touchUpInside)
return cell
}
else {
let cell: RecommendToFriendsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "RecommendToFriendsCell") as! RecommendToFriendsTableViewCell
let rowNumber = (indexPath as NSIndexPath).row
// set the content view background color
cell.contentView.backgroundColor = tableViewBgColor
// set the text color
cell.nameLabel.textColor = textColor
var dict_friend = NSMutableDictionary()
if searchActive {
dict_friend = filteredFriendsArray[rowNumber]
}
else {
dict_friend = friendsArray[rowNumber]
}
let name = dict_friend["name"] as! String
cell.nameLabel.text = name
let friendUID = dict_friend["uid"] as! String
cell.friendID = friendUID
let imageAddress = dict_friend["photo"] as? String
if imageAddress != "unavailable" && imageAddress != nil && imageAddress != ""{
//Swift forces us to wrap strings as optional to use them in logic
if let imageURL = imageAddress as String? {
//Swift forces us to wrap strings as optional to use them in logic
if let image = imageURL as String? {
//We convert the string into a URL and get the image
let url = URL(string: image)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
//We create a new async thread to download and update the image
DispatchQueue.main.async {
//imageView.image = UIImage(data: data!)
cell.photoImageView.image = UIImage(data:data!)
}
}).resume()
}
} else {
cell.photoImageView!.image = UIImage(named: "placeholder-profile-male.png")
}
} else {
cell.photoImageView!.image = UIImage(named: "placeholder-profile-male.png")
}
cell.checkBoxImageView.image = cell.checkBoxImageView.image!.withRenderingMode(.alwaysTemplate)
cell.checkBoxImageView.tintColor = textColor
// Style the profile photo to show in a circle
cell.photoImageView.layer.borderWidth = 0
cell.photoImageView.layer.borderColor = tableViewBgColor.cgColor
// Set cornerRadius = a square UIImageView frame size width / 2
// In our case, UIImageView height = width = 60 points
cell.photoImageView.layer.cornerRadius = 30
cell.photoImageView.clipsToBounds = true
cell.selectionStyle = .none // to prevent cells from being "highlighted"
return cell
}
}
This is the method where we interact with them. The crash happens on a cellForRow call for a cell that is out of view (aka dequeued)
var firstFriendName: String = ""
var numberOfFriends = 0
if let selectedRow = recommendToFriendTableView.indexPathsForSelectedRows {
numberOfFriends = selectedRow.count
for i in 0..<selectedRow.count {
let currentCell = recommendToFriendTableView.cellForRow(at: selectedRow[i]) as! RecommendToFriendsTableViewCell
let friendID = currentCell.friendID
idList.append(",\(friendID)")
}
let firstSelectedCell = recommendToFriendTableView.cellForRow(at: selectedRow[0]) as! RecommendToFriendsTableViewCell
firstFriendName = firstSelectedCell.nameLabel.text!
After about a day of experimenting, I've yet to figure out the actual problem (other than the observation that it appears to be in regards to calling a dequeued cell)
Any help is appreciated.
When this line
let currentCell = recommendToFriendTableView.cellForRow(at: selectedRow[i]) as! RecommendToFriendsTableViewCell
crashes this means you access a non-visble cell so either use
if let currentCell = recommendToFriendTableView.cellForRow(at: selectedRow[i]) as? RecommendToFriendsTableViewCell { }
or better use the dataSource array of the table to get the data that you want to gran wrongly from the cell

File uploading progress issue

I am uploading a single file in a table view cell using AFNetworking.
Uploading is working alright. But when I scroll my cell off view, the progress is gone or some times it displays different progress values like 20% or 50%, and again 30%.
This is my code:
//Tableview Cell configure
var cell : ChattingPhotoCell!
if message.vendorType == .Receiver {
//Receive Image message
cell = tableView.dequeueReusableCell(withIdentifier: "ReceiveChattingPhotoCell") as! ChattingPhotoCell
if cell == nil {
cell = Bundle.main.loadNibNamed("ReceiveChattingPhotoCell", owner: self, options: nil)?[0] as! ChattingPhotoCell
}
cell.reloadDelegate = self
cell.mDelegate = self
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.conficureImageCell(msg: message,indexPath: indexPath)
} else {
// Send Image Message
cell = tableView.dequeueReusableCell(withIdentifier: "SenderChattingPhotoCell") as! ChattingPhotoCell
if cell == nil {
cell = Bundle.main.loadNibNamed("SenderChattingPhotoCell", owner: self, options: nil)?[0] as! ChattingPhotoCell
}
cell.reloadDelegate = self
cell.mDelegate = self
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.conficureImageCell(msg: message,indexPath: indexPath)
}
//MyCell code
func conficureImageCell(msg:Message,indexPath:IndexPath) {
self.message = msg
self.indexPath = indexPath
if self.message.vendorType == .Sender {
self.senderCellConfigure()
} else {
self.receiverCellConfigure()
}
}
// sender configure methods
func senderCellConfigure() {
// Send Message
if message.upload == 1 {
self.btnRetry.isHidden = true
}else{
self.btnRetry.isHidden = false
}
if message.is_send == 0 && !self.message.isUploadMedia && message.upload != 1 {
let image = UIImage.init(contentsOfFile: documentDir.path)
if image != nil {
self.uploadImageToServer(ArrImage_Video: NSMutableArray.init(array: [image!]), strMsgType: "3")
}
}
if self.message.isUploadMedia {
self.progressView.isHidden = false
self.btnRetry.isHidden = true
} else {
self.progressView.isHidden = true
}
}
// MARK:- WebserviceCalling // Hiren
func uploadImageToServer(ArrImage_Video:NSMutableArray,strMsgType: String){
self.message.isUploadMedia = true
self.btnRetry.isHidden = true
if self.str_media_url.isBlank {
self.progressView.isHidden = false
let accessToken = Singleton.sharedSingleton.retriveFromUserDefaults(key:Global.kLoggedInUserKey.AccessToken)
print(accessToken!)
let param: NSMutableDictionary = NSMutableDictionary()
param.setValue(fromJID, forKey: "from_user_id")
param.setValue(toJID, forKey: "to_user_id")
param.setValue(strMsgType, forKey: "message_type")
param.setValue("ABC", forKey: "from_user_name")
param.setValue(UIDevice.current.model, forKey: "device_type")
param.setValue("897584acac541d73d5f01f294fe944ddb35b6f67ea894e9ac29b03c7da69ca48", forKey: "device_token")
param.setValue("jpeg", forKey: "file_type")
param.setValue("135", forKey: "message_id")
param.setValue(accessToken, forKey: "access_token")
AFAPIMaster.sharedAPIMaster.PostMediatoServer_chat(params: param, arrMedia: ArrImage_Video, showLoader: false, enableInteraction: true, viewObj: self, onSuccess: {
(DictResponse) in
let dictResponse: NSDictionary = DictResponse as! NSDictionary
let dictData: NSDictionary = dictResponse.object(forKey: "SuccessResponse") as! NSDictionary
let media_url = dictData.object(forKey: "media_url") as? String ?? ""
let media_id = dictData.object(forKey: "id") as? Int ?? 0
let thumb_image = dictData.object(forKey: "thumb_image") as? String ?? ""
print(media_url)
print(thumb_image)
DispatchQueue.main.async {
self.progressView.isHidden = true
let onChatMaster = OnChatMasterTable()
let messageObj = onChatMaster.getMessageFromDB(strMessageID: self.msgID, strFromUId: self.fromJID, strToUId: self.toJID)
messageObj.media_url = media_url
messageObj.thumb_url = thumb_image
messageObj.upload = 1
messageObj.message = self.imageName
messageObj.media_id = media_id
self.str_media_ID = String(media_id)
self.message.media_id = media_id
DBHelper.sharedInstance.queue?.inDatabase() {
db in
DBHelper.sharedInstance.chilaxDB.open()
let strUpdQuery = "UPDATE OnChat_Master SET upload = 1 , media_url = '\(media_url)', thumb_url = '\(thumb_image)' , media_id = \(media_id) where message_id = '\(messageObj.message_id)' AND from_user_id = '\(messageObj.from_user_id)' AND to_user_id = '\(messageObj.to_user_id)'"
DBHelper.sharedInstance.chilaxDB.executeUpdate(strUpdQuery, withArgumentsIn: [])
DBHelper.sharedInstance.chilaxDB.close()
}
//onChatMaster.updateMessageInDB(messsageObj: messageObj)
self.sendMediaDataToSender(media_url: media_url, thumb_image: thumb_image,strMediaId:self.str_media_ID)
self.message.isUploadMedia = false
}
}, displayProgress: {
(progress) in
DispatchQueue.main.async {
let progressTask : Float = Float((progress as! Progress).fractionCompleted)
print(progressTask)
self.progressView.setProgress(progressTask, animated: true)
}
}, onFailure: {
self.progressView.setProgress(0.0, animated: true)
self.progressView.isHidden = true
self.btnRetry.isHidden = false
self.message.isUploadMedia = false
})
} else {
self.sendMediaDataToSender(media_url: str_media_url, thumb_image: str_thumb_url, strMediaId: str_media_ID)
}
}
I reuse cell using table view deque methods. Any wrong in this code.

Automatically saving changes in a cell to object when editing finishes?

im having a real nightmare with my project where i need to save cell contents to an object, for each object in an array. I cant get this to work by looping through table cells adn array objects and trying to match them all up.
So my next idea was to add didFinishEditing related functions into the cellForRowAt function?
Im not sure this would work either, but this is what i have:
Each row here has a label for the set, a picker for the reps that can be scrolled to a number, and a textfield to put a weight. Then i save each row as an object storing the set, rep and weight.
Issue is when editing this, how can i save these again overwriting the old values? Hence my plan above to use didFinishEditing methods.
My previous plan was the code below, but i cant figure out the annotated part. So i was hoping someone had guidance on how i can approach saying when editing rather than this save button function that doesnt work!
func saveUserExerciseSets() {
if userExercise == nil {
print("CREATING A FRESH SET OF SETS FOR THE NEW EXERCISE")
for cell in self.customSetsTable.visibleCells as! Array<NewExerciseTableViewCell> {
print("SAVING THESE CELLS \(customSetsTable.visibleCells)")
let newUserExerciseSet = UserExerciseSet(context: self.managedObjectContext)
newUserExerciseSet.setPosition = Int64(cell.setNumber.text!)!
newUserExerciseSet.setReps = Int64(cell.repsPicker.selectedRow(inComponent: 0))
newUserExerciseSet.parentExerciseName = self.userExerciseName.text
if self.localeIdentifier == "en_GB" {
let kgWeight = Measurement(value: Double(cell.userExerciseWeight.text!)!, unit: UnitMass.kilograms)
newUserExerciseSet.setWeight = kgWeight as NSObject?
newUserExerciseSet.initialMetricSystem = self.localeIdentifier
} else if self.localeIdentifier == "en_US" {
let lbsWeight = Measurement(value: Double(cell.userExerciseWeight.text!)!, unit: UnitMass.pounds)
newUserExerciseSet.setWeight = lbsWeight as NSObject?
newUserExerciseSet.initialMetricSystem = self.localeIdentifier
}
let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "name == %#", self.exerciseNameToAddTo!)
do {
let parentExercise = try self.managedObjectContext.fetch(fetchRequest).first
parentExercise?.addToExercisesets(newUserExerciseSet)
print("SET ADDED TO EXERCISE")
} catch {
print("Fetching Routine Failed")
}
}
} else if self.userExercise != nil {
print("UPDATING EXISTING SETS FOR THE EXISTING EXERCISE")
let cells = self.customSetsTable.visibleCells as! Array<NewExerciseTableViewCell>
for cell in cells {
let exerciseSets = self.userExercise?.exercisesets?.allObjects as! [UserExerciseSet]
let sortedexerciseSets = exerciseSets.sorted { ($0.setPosition < $1.setPosition) }
let cellsSet = sortedexerciseSets //match the sortedexerciseSets set object to the cell index positions
cellsSet.setPosition = Int64(setsCell.setNumber.text!)!
cellsSet.setReps = Int64(setsCell.repsPicker.selectedRow(inComponent: 0))
if self.localeIdentifier == "en_GB" {
let kgWeight = Measurement(value: Double(cell.userExerciseWeight.text!)!, unit: UnitMass.kilograms)
cellsSet.setWeight = kgWeight as NSObject?
} else if self.localeIdentifier == "en_US" {
let lbsWeight = Measurement(value: Double(cell.userExerciseWeight.text!)!, unit: UnitMass.pounds)
cellsSet.setWeight = lbsWeight as NSObject?
}
cellsSet.parentExerciseName = self.userExerciseName.text
}
}
do {
try self.managedObjectContext.save()
print("THE SET HAS BEEN SAVED")
} catch {
fatalError("Failure to save context: \(error)")
}
delegate?.didFinishEditing()
self.dismiss(animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NewExerciseTableViewCell
else {
fatalError("Unexpected Index Path")
}
cell.backgroundColor = UIColor.customBackgroundGraphite()
cell.textLabel?.textColor = UIColor.white
cell.repsPicker.dataSource = self
cell.repsPicker.delegate = self
configure(cell, at: indexPath)
return cell
}
func configure(_ cell: NewExerciseTableViewCell, at indexPath: IndexPath) {
// configuring cells when theres a loaded exercise causes the issues --------------------
if self.userExercise != nil {
print("RESTORING CELLS FOR THE EXISTING EXERCISE")
let unsortedExerciseSets = self.userExercise?.exercisesets?.allObjects as! [UserExerciseSet]
let exerciseSets = unsortedExerciseSets.sorted { ($0.setPosition < $1.setPosition) }
let cellsSet = exerciseSets[indexPath.row]
cell.setNumber.text = String((indexPath.row) + 1)
let indexRow = Int(cellsSet.setReps)
print("INDEX ROW INT IS \(indexRow)")
cell.repsPicker.selectRow(indexRow, inComponent: 0, animated: true) //fix this crashing issue!
let localeIdentifier = Locale(identifier: UserDefaults.standard.object(forKey: "locale") as! String)
let setWeight = cellsSet.setWeight as! Measurement<UnitMass>
let formatter = MassFormatter()
formatter.numberFormatter.locale = localeIdentifier
formatter.numberFormatter.maximumFractionDigits = 2
if localeIdentifier.usesMetricSystem {
let kgWeight = setWeight.converted(to: .kilograms)
let finalKgWeight = formatter.string(fromValue: kgWeight.value, unit: .kilogram)
let NumericKgResult = finalKgWeight.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.").inverted)
cell.userExerciseWeight.text = NumericKgResult
} else {
let lbsWeight = setWeight.converted(to: .pounds)
let finalLbWeight = formatter.string(fromValue: lbsWeight.value, unit: .pound)
let NumericLbResult = finalLbWeight.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.").inverted)
cell.userExerciseWeight.text = NumericLbResult
}
} else if self.userExercise == nil {
print("NEW SET CELL ADDED FOR FRESH EXERCISE")
cell.setNumber.text = String((indexPath.row) + 1)
}
}
Try something like this to match the setIds correctly. That's where I think the issue is.
for x in sortedexerciseSets {
if x.setPosition == Int64(setsCell.setNumber.text!)! {
//save
}
}
Proper way to do it would be to have an array of those sets (I guess, since you tagged core-data, they are instances of NSManagedObject?). When user does ANY change in the cell (write new value in the text field or scroll to another value for reps) you need to update the approproate object in your array immediately. Then you could call save on NSManagedObjectContext when you're sure you want to save changes, or just call rollback on the context to cancel all changes.

How to filter an array of JSON objects to be used in a table view?

I'm getting JSON data from an API and parsing that data in objects, which are then simply stored in an array of objects. The objects themselves contain data about articles from a newspaper. However, I need to filter that data. Some of the objects I'm getting from my JSON actually have no article content because they are pictures and not articles (i.e. some of the "nodes" from the API's JSON have content that I don't want to see in my table view).
In my JSON-parsing function, I've tried to make it so that the parsed object will only get added to the array of parsed objects if the character count of the "articleContent" variable is above 40. Here is what it looked like.
if issueElement.articleContent.characters.count > 40 {
self.currentIssueObjects.addObject(issueElement)
}
However, this simply does not work. I get the typical "unexpectedly found nil while unwrapping an Optional value" error message (I don't get a specific line for the error). How can I make this work ? I'm essentially trying to prevent the array from having objects with empty articleContent, because then that screws up my table view (empty cells, duplicates, etc...).
Here is my cellForRowAtIndexPath code, and my JSON-parsing code:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
let row = indexPath.row
guard let cell = tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as? CurrentIssueArticlesTableViewCell else {
print ("error: currentIssueTableView cell is not of class CurrentIssueArticlesTableViewCell, we will use EditorialsTableViewCell instead")
return tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell
}
let currentIssueObject = currentIssueObjects.objectAtIndex(indexPath.row) as! IssueElement
let title = currentIssueObject.title ?? ""
let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(currentIssueObject.timeStamp))
let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject) ?? "Date unknown"
if let author = currentIssueObject.author {
cell.currentIssueArticlesAuthorLabel!.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.currentIssueArticlesAuthorLabel!.text = author
}
let issueNumber = currentIssueObject.issueNumber ?? ""
let volumeNumber = currentIssueObject.volumeNumber ?? ""
let articleContent = currentIssueObject.articleContent ?? ""
let nodeID = currentIssueObject.nodeID ?? 0
cell.currentIssueArticlesHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.currentIssueArticlesHeadlineLabel.text = title
cell.currentIssueArticlesPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.currentIssueArticlesPublishDateLabel.text = timeStampDateString
if row == 0 {
cell.userInteractionEnabled = false
let imageURL = (currentIssueObjects.objectAtIndex(row) as! IssueElement).imageURL
cell.currentIssueArticlesHeadlineLabel.textColor = UIColor.clearColor()
cell.currentIssueArticlesAuthorLabel.textColor = UIColor.clearColor()
cell.currentIssueArticlesPublishDateLabel.textColor = UIColor.clearColor()
cell.request?.cancel()
if let image = self.imageCache.objectForKey(imageURL!) as? UIImage {
cell.currentIssueArticlesBackgroundImageView.image = image
} else {
cell.currentIssueArticlesBackgroundImageView.image = UIImage(named: "reveal Image")
cell.request = Alamofire.request(.GET, imageURL!).responseImage() { response in
if response.result.error == nil && response.result.value != nil {
self.imageCache.setObject(response.result.value!, forKey: response.request!.URLString)
cell.currentIssueArticlesBackgroundImageView.image = response.result.value
} else {
}
}
}
cell.currentIssueArticlesBackgroundImageView.hidden = false
}
else {
cell.currentIssueArticlesBackgroundImageView.hidden = true
}
return cell
}
JSON-parsing code:
func populateCurrentIssue() {
if populatingCurrentIssue {
return
}
populatingCurrentIssue = true
self.cellLoadingIndicator.backgroundColor = goldenWordsYellow
self.cellLoadingIndicator.startAnimating()
Alamofire.request(GWNetworking.Router.Issue).responseJSON() { response in
if let JSON = response.result.value {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
var nodeIDArray : [Int]
if (JSON .isKindOfClass(NSDictionary)) {
for node in JSON as! Dictionary<String, AnyObject> {
let nodeIDValue = node.0
var lastItem : Int = 0
self.nodeIDArray.addObject(nodeIDValue)
if let issueElement : IssueElement = IssueElement(title: "Could not retrieve title", nodeID: 0, timeStamp: 0, imageURL: "init", author: "Author not found", issueNumber: "Issue # error", volumeNumber: "Volume # error", articleContent: "Could not retrieve article content", coverImageInteger: "init", coverImage: UIImage()) {
issueElement.title = node.1["title"] as! String
issueElement.nodeID = Int(nodeIDValue)!
let timeStampString = node.1["revision_timestamp"] as! String
issueElement.timeStamp = Int(timeStampString)!
issueElement.imageURL = String(node.1["image_url"])
if let author = node.1["author"] as? String {
issueElement.author = author
}
if let issueNumber = node.1["issue_int"] as? String {
issueElement.issueNumber = issueNumber
}
if let volumeNumber = node.1["volume_int"] as? String {
issueElement.volumeNumber = volumeNumber
}
if let articleContent = node.1["html_content"] as? String {
issueElement.articleContent = articleContent
}
issueElement.coverImageInteger = String(node.1["cover_image"]) // addition specific to the Current Issue View Controller
lastItem = self.currentIssueObjects.count
print(issueElement.nodeID)
if issueElement.articleContent.characters.count > 40 {
self.currentIssueObjects.addObject(issueElement)
print(issueElement.nodeID)
}
// Sorting with decreasing timestamp from top to bottom.
let timestampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
self.currentIssueObjects.sortUsingDescriptors([timestampSortDescriptor])
// Placing the object with coverImage
let coverImageSortDescriptor = NSSortDescriptor(key: "coverImageInteger", ascending: false)
self.currentIssueObjects.sortUsingDescriptors([coverImageSortDescriptor])
let indexPaths = (lastItem..<self.currentIssueObjects.count).map {
NSIndexPath(forItem: $0, inSection: 0) }
}
}
}
dispatch_async(dispatch_get_main_queue()) {
self.currentIssueTableView.reloadData()
self.cellLoadingIndicator.stopAnimating()
self.cellLoadingIndicator.hidesWhenStopped = true
}
}
}
self.populatingCurrentIssue = false
}
}

Resources