Edgy Scrolling on UITableView - iOS - ios

I'm making an application that populates the table view from a parse database. The problem is there's a bit of lag whenever I scroll. Through some searching, I've come to realize that the problem lies in the cellForRowAtIndexPath() function where I'm querying the database in order to get the data I need for that cell's labels. I have to query the database there because I use the cell's position to index into my messages array in order to grab the right message. Here's the meat of my cellForRowAtIndexPath() function:
if (path.row < messagesArray.count) {
var message = messagesArray[path.row]
var dateFormat = NSDateFormatter()
var query = PFQuery(className: "Messages")
query.whereKey("messageID", equalTo: message.messageID)
var first = query.getFirstObject()
if (message.senderID == loggedInUserID) {
cell.nameLabel?.text = "I said"
} else {
queryO?.whereKey("objectId", equalTo: message.senderID)
var second = queryO?.getFirstObject()
var name = second!.objectForKey("FIRST") as! String
var middle = second!.objectForKey("MIDDLE") as! String?
var last = second!.objectForKey("LAST") as! String
if (middle != nil) {
cell.nameLabel?.text = name + " " + middle! + " " + last
} else {
cell.nameLabel?.text = name + " " + last
}
}
cell.messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.messageLabel?.text = message.messageText
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy 'at' h:mm a" // superset of OP's format
let str = dateFormatter.stringFromDate(first!.objectForKey("TIME") as! NSDate)
cell.senderLabel?.text = str
}
Any ideas?

What I ended up doing was I took the query outside my function and ran it in my viewDidLoad() which improved the speed drastically. Thanks for your help guys :-)

Related

how to organize tableview cells

I have two different kinds of tableview cells in one table view. The first cell prints out original comments to a post, the second cell prints out comments to another comment. Currently, the tableview prints out all the correct cells in no particular order. However, I want to print the cells in a particular order. I want the cells that contain comments to another comment to appear below the comment it is being commented on.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCell(withIdentifier: "Main", for: indexPath) as! PostTableViewCell
//Configure the cell
cell.PostView.layer.cornerRadius = 5
cell.PostView.layer.masksToBounds = false
cell.PostView.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.PostView.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.PostView.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
let commentname = post["author"] as? String
sendAuthor = post["author"] as? String
cell.CommentersName.setTitle(commentname, for: .normal)
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
let formating = timeStampDate as Date
cell.CommentTime.text = dateFormatter.string(from: formating)
}
cell.comment.text = post["content"] as? String
textViewDidChange(cell.comment)
cell.comment.frame.size.width = 344
cell.comment.sizeToFit()
cell.comment.clipsToBounds = true
cell.REply.frame.origin.y = cell.comment.frame.maxY + 10
cell.report.frame.origin.y = cell.comment.frame.maxY + 10
cell.Likes.frame.origin.y = cell.comment.frame.maxY + 10
cell.LikesNumber.frame.origin.y = cell.comment.frame.maxY + 10
cell.PostView.frame.size.height = cell.comment.frame.maxY + 50
TableView.rowHeight = cell.PostView.frame.size.height + 20
cell.CommentersName.sizeToFit()
cell.pole.frame.origin.x = cell.CommentersName.frame.maxX + 5
cell.CommentTime.frame.origin.x = cell.pole.frame.maxX + 5
let numLikes = post["num_likes"] as? NSNumber
cell.LikesNumber.text = String(describing: numLikes!)
replyId = post["id"] as? String
let replyTo = post["reply_to"] as? String
let postID = post["post_id"] as? String
if replyTo == postID {
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reply", for: indexPath) as! RepliesTableViewCell
cell.ReplyCustomCell.layer.cornerRadius = 5
cell.ReplyCustomCell.layer.masksToBounds = false
cell.ReplyCustomCell.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.ReplyCustomCell.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.ReplyCustomCell.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
cell.ReplyText.text = post["content"] as? String
let commentname = post["author"] as? String
cell.author.setTitle(commentname, for: .normal)
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
let formating = timeStampDate as Date
cell.time.text = dateFormatter.string(from: formating)
}
let numLikes = post["num_likes"] as? NSNumber
cell.num_likes.text = String(describing: numLikes!)
textViewDidChange(cell.ReplyText)
cell.ReplyText.frame.size.width = 232
cell.ReplyText.sizeToFit()
cell.ReplyText.clipsToBounds = true
cell.author.sizeToFit()
cell.pole.frame.origin.x = cell.author.frame.maxX + 5
cell.time.frame.origin.x = cell.pole.frame.maxX + 5
cell.Likes.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.num_likes.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.reportButton.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.replyButton.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.ReplyCustomCell.frame.size.height = cell.ReplyText.frame.maxY + 50
TableView.rowHeight = cell.ReplyCustomCell.frame.size.height + 20
return cell
}
cell.checkfornightmode()
return cell
}
The comments that are associated with each other have the same "id", how will I organize the cells so that the comments of a main comment will be listed under the original comment. Thank you
You can create one Comment custom object class which will hold an array of sub comments and the main comment to arrange or manage your data structure properly. After that you can use it properly with your table view cell.
Okay so for example you can have the below data structure.
Create one Comment class:
class Comment {
comment_id
content
post_id
reply_to
}
Now create one more class for your table view:
class CommentTableDataModel {
var mainComment: Comment // Of type Comment class
var replies: [Comment] // Array of type Comment class for sub comments
}
So now just iterate through your firebase Comments array and prepare an array list of type 'CommentTableDataModel' objects as a datasource for your table. So finally you will have an array of type object 'CommentTableDataModel' and each object of type 'CommentTableDataModel' contains the main comment info as well as the list of replies info with that, with this you can manage your data.

Dynamically using specific variables with custom UITableViewCell

This question will be a follow-up of this previous one.
I'm at the point where the user can create UITableViewCells and enter an amount to a TextField. It is then printed to a label and should update a variable that will be used with another variable for a calculation (basically amount * currentPrice, this is a wallet).
I re-read Apple's doc about TableViews and re-opened the exercises I did when I followed the Swift course but I'm struggling to understand the principle here, so once again, I think I really need someone to explain differently than what I could read so my brain can understand.
How will the cell know what variable to use here:
Entering the amount:
func cellAmountEntered(_ walletTableViewCell: WalletTableViewCell) {
if walletTableViewCell.amountTextField.text == "" {
return
}
let str = walletTableViewCell.amountTextField.text
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "en_US")
let dNumber = formatter.number(from: str!)
let nDouble = dNumber!
let eNumber = Double(truncating: nDouble)
walletTableViewCell.amountLabel.text = String(format:"%.8f", eNumber)
UserDefaults.standard.set(walletTableViewCell.amountLabel.text, forKey: "cellAmount")
walletTableViewCell.amountTextField.text = ""
}
Calculations and display:
func updateCellValueLabel(cryptoPrice: String) {
if walletTableViewCell.amountLabel.text == "" {
walletTableViewCell.amountLabel.text = "0.00000000"
}
let formatter1 = NumberFormatter()
formatter1.locale = Locale(identifier: "en_US")
let str = walletTableViewCell.amountLabel.text
let dNumber = formatter1.number(from: str!)
let nDouble = dNumber!
let eNumber = Double(truncating: nDouble)
UserDefaults.standard.set(eNumber, forKey: "currentAmount")
guard let cryptoDoublePrice = CryptoInfo.cryptoPriceDic[cryptoPrice] else { return }
WalletViewController.bitcoinAmountValue = cryptoDoublePrice * eNumber
if WalletViewController.currencyCode != "" {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "\(WalletViewController.currencyCode)")
walletTableViewCell.cryptoValueLabel.text = formatter.string(from: NSNumber(value: WalletViewController.amountValue))
}
UserDefaults.standard.set(WalletViewController.walletDoubleValue, forKey: "walletValue")
}
What I can't process here is how can I save the amount entered to the corresponding crypto UserDefaults file, then load this amount to be used with the corresponding variable that has the current price for calculations.
I don't know how to use the correct data for the correct cell.
Should I make this a single function? Should I pass parameters to the function (how do I pass the correct parameters to the corresponding cell with delegate?)? Do I need an array of parameters that will be used at indexPath to the correct cell (but how do I know the order if the user creates cells at will?)?
I'm kind of lost here.

how to ignore a value of a UITextField that has already been calculated in swift 3?

I'm trying to make an app that is very basic. One part of the app is that there are 4 textFields and a button that calculates the sum of these textFields.
The problem that I'm facing is that say I type the value 10 in the first textField then I press the button. The result would be 10. However, if I press it again ( without typing anything in the other textFields), the result would be 20!! Furthermore, if I type 20 in one of the other textFields, the result would be 40!!
The result SHOULD BE 30 NOT 40!!
one possible option I thought of (haven't tried it yet) is assigning 0 to all of the textFields when pressing the button. But I'd like the app to be smarter and keep tracks of the result.
if it helps, here's the code inside the button that calculates the sum:
#IBAction func calBtnPressed(_ sender: UIButton) {
var benifit:[Double] = []
var textFields: [Double] = []
if initialBalance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = initialBalance.text {
// these lines of code will convert arabic numbers to English ones in case the user uses Arabic number
let initialStr: String = temp
let initialFormatter: NumberFormatter = NumberFormatter()
initialFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let initialFinal = initialFormatter.number(from: initialStr)
benifit.append(Double(initialFinal!))
}
}
if income.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = income.text {
// these lines of code will convert Arabic numbers to English ones in case the user uses Arabic number
let incomeStr: String = temp
let incomeFormatter: NumberFormatter = NumberFormatter()
incomeFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let incomeFinal = incomeFormatter.number(from: incomeStr)
benifit.append(Double(incomeFinal!))
}
}
if salaries.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = salaries.text {
let salariesStr: String = temp
let salariesFormatter: NumberFormatter = NumberFormatter()
salariesFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let salariesFinal = salariesFormatter.number(from: salariesStr)
textFields.append(Double(salariesFinal!))
}
}
if tools.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = tools.text {
let toolsStr: String = temp
let toolsFormatter: NumberFormatter = NumberFormatter()
toolsFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let toolsFinal = toolsFormatter.number(from: toolsStr)
textFields.append(Double(toolsFinal!))
}
}
if maintinance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = maintinance.text {
let maintinanceStr: String = temp
let maintinanceFormatter: NumberFormatter = NumberFormatter()
maintinanceFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let maintinanceFinal = maintinanceFormatter.number(from: maintinanceStr)
textFields.append(Double(maintinanceFinal!))
}
}
if other.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = other.text {
let otherStr: String = temp
let otherFormatter: NumberFormatter = NumberFormatter()
otherFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let otherFinal = otherFormatter.number(from: otherStr)
textFields.append(Double(otherFinal!))
}
}
for textField in textFields {
sumExpenses += textField
}
for ben in benifit{
sumBenifit += ben
}
totalExpenses.text = String(sumExpenses)
totalAfterSubtractingExpenses.text = String( sumBenifit - sumExpenses )
sumBenifit -= sumExpenses
}
I think I found your problem.
You use a variable sumBenefit which isn't declared in your func, so I assume it is declared in your UIViewController.
Since it is an instance variable, it will not reset each time you click the button.
If you want to reset the values of sumExpenses and sumBenefits each time the button is pressed, then you'll have to do something like this:
sumExpenses = 0
for textField in textFields {
sumExpenses = Int(textField.text)!
}
sumBenefit = 0
for ben in benefit {
sumBenefit += ben
}
I am also making the assumption that you want a number from your textField in the first for-loop, because if sumExpenses is of type Int (or any other number for that matter) then sumExpenses += textField will not compile. You need to take the text of that textField and convert it to an Int.
Again, I am still not super clear what you are trying to do, but please let me know if this works for you, or if you need further clarification.

Array of custom object is not being set

I am trying to store an Object, CustomPinAnnotation into an array. I have declared it like this: var pinArray = [CustomPinAnnotation]()
I am calling Firebase in a function which gets a lot of locations in which I construct my CustomPin.
func loadStuff()
_REF_BASE.child("Objects").observeSingleEventOfType(.Value, withBlock: { snapshot in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot]{
for snap in snapshots {
if let dict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let obj = objModel(lobbyKey: key, dictionary: pokeDict)
for location in obj.locations {
let coord = location.locationCoordinate
let customPin = CustomPinAnnotation()
customPin.coordinate = coord.coordinate
customPin.title = obj.name
let time = NSDate(timeIntervalSince1970: location.time)
let dayTimePeriodFormatter = NSDateFormatter()
dayTimePeriodFormatter.dateFormat = "MMM dd YYYY hh:mm a"
let dateString = dayTimePeriodFormatter.stringFromDate(time)
customPin._detailedDescription = "Seen at " + dateString
customPin.subtitle = "Seen at \(dateString)"
customPin._pokemonName = obj.name
customPin._imageName = obj.name
customPin._successRate = location.successRate
customPin._upVotes = location.upVotes
customPin._downVotes = location.downVotes
customPin._caughtAtTime = dateString
self.pinToPass = customPin // send this custom pin with info
self.pinArray.append(customPin)
// self.mapView.addAnnotation(customPin);
}
}
}
}
}
})
My ViewDidLoad method
self.PokeMapView.zoomEnabled = true
loadStuff()
self.mapView.addAnnotations(pinArray)
self.mapView.showAnnotations(pinArray, animated: true)
The commented line //self.MapView.addAnnotation(customPin) currently works, but this is very inefficient, as it would pin objects on the map that are not in the current scope of the map itself. However, when I try adding the list of Annotations like so: MapView.addAnnotations(pinArray), nothing works and via the debugger i'm told that the array contains 0 elements.
It'd be greatly appreciated if somebody could help me, as i've been stuck on this for a while and it really causes poor performance.

How to check NSDate yyyy/mm/dd in NSArray and compare it?

I have an NSArray, inside I have a var of kind NSDate
which give this format "timeStamp = "2015-08-18 16:58:31"
I want to compare in all the array the date only 2015-08-18
compare it and if same date only the first one show full NSDate
and the rest with same date show only the time on UI
This is what I did so far:
func getAllMessages() -> NSArray{
var allMessages = self.mutableSetValueForKey("messages").allObjects as NSArray
let timeStampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: true)
var sortByTime = allMessages.sortedArrayUsingDescriptors([timeStampSortDescriptor])
println("\(sortByTime)")
return sortByTime
}
screen shot
http://i.stack.imgur.com/wcsSz.jpg
After some research i did made a solution as following
First this extension is very good and my way is depends on it
https://stackoverflow.com/a/27369380/5188737
If anyone have better solution or clean
I will appreciate if you can post it here
I'm sorry there's no comments
func loadMessages(){
if chatRoom!.messages.count > 0{
var i = 0
var tempMsgDate:NSDate?
var chatMessage:ChatMessage?
for message in chatRoom!.getAllMessages(){
let msg = message as! Message
if i == 0{
tempMsgDate = msg.timeStamp
chatMessage = ChatMessage(incoming: msg.income, text: msg.message, sentDate: convertNSDateToString(msg.timeStamp))
i++
}else{
//if the tempMsgDate (which is the first of the same
//date in the nsarray)
if checkIfSameDayDate(tempMsgDate!,date2: msg.timeStamp){
var tempDate = msg.timeStamp.time
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
let date = dateFormatter.dateFromString(tempDate)
println("loadmessages method: \(date?.time)")
chatMessage = ChatMessage(incoming: msg.income, text: msg.message, sentDate: msg.timeStamp.time)
}else{//after he got to different date it save it as
//first of same date to follow
tempMsgDate = msg.timeStamp
chatMessage = ChatMessage(incoming: msg.income, text: msg.message, sentDate: convertNSDateToString(msg.timeStamp))
}
}
var msgCollection:[ChatMessage] = [ChatMessage]()
msgCollection.append(chatMessage!)
chat.loadedMessages.append(msgCollection)
}
}
}
func convertNSDateToString(date:NSDate) -> String{
let dateString = date.date + " " + date.time
println("in convert: \(dateString)")
return dateString
}
func checkIfSameDateFromSendMSGAndReciveMSG(date:NSDate) -> Bool{
for message in chatRoom!.getAllMessages(){
let msg = message as! Message
if msg.timeStamp.date == date.date{
return true
}
}
return false
}
//checks if the date1 equal to date2
func checkIfSameDayDate(date1:NSDate,date2:NSDate) -> Bool{
if date1.date == date2.date{
return true
}
return false
}

Resources