ProgressView progress in TableViewCell is altered when table view is scrolled - ios

In my app I have to show multiple progress views in a single tableViewCell, the progress as per actual to goal and it showing correctly when cell is loaded for the first time as per image1
but after scrolling the table the cell is visible like image2.
The progress of the progress views is altered after scrolling the tableview. I have printed the progress values in cellForRowAt method and they are correct but the progress in progress view is being altered. I am setting the progressview progress in cellForRowAt method.
Can anybody help me to resolve this issue?
#Bhavin Kansagara here is my code in cellForRowAt method.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cellIdentifier = "myIdentifier"
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
if cell == nil {
tableView.register(UINib.init(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
}
let myDict = self.myArray[indexPath.row]
let goalDict = myDict["goal"]!
let actualDict = myDict["actual"]!
for label in cell!.actualValueLabels {
switch label.tag {
case 0:
label.text = String(actualDict["value1"]!)
case 1:
label.text = String(actualDict["value2"]!)
case 2:
label.text = String(actualDict["value3"]!)
case 3:
label.text = String(actualDict["value4"]!)
case 4:
label.text = String(actualDict["value5"]!)
case 5:
label.text = String(actualDict["total"]!)
default:
break
}
}
for label in cell!.goalValueLabels {
switch label.tag {
case 0:
label.text = String(goalDict["value1"]!)
case 1:
label.text = String(goalDict["value2"]!)
case 2:
label.text = String(goalDict["value3"]!)
case 3:
label.text = String(goalDict["value4"]!)
case 4:
label.text = String(goalDict["value5"]!)
case 5:
label.text = String(goalDict["total"]!)
default:
break
}
}
let greenColor = UIColor(red: 146/255, green: 209/255, blue: 78/255, alpha: 1)
let yellowColor = UIColor(red: 1, green: 122/255, blue: 0, alpha: 1)
let value1Goal = goalDict["value1"]!
let value1Actual = actualDict["value1"]!
cell?.value1ProgressView.progressTintColor = value1Actual >= value1Goal ? greenColor : yellowColor
if value1Goal == 0 {
DispatchQueue.main.async {
cell?.value1ProgressView.setProgress(Float(value1Actual), animated: true)
}
} else {
DispatchQueue.main.async {
cell?.value1ProgressView.setProgress(Float(value1Actual)/Float(value1Goal), animated: true)
}
}
let value2Goal = goalDict["value2"]!
let value2Actual = actualDict["value2"]!
cell?.value2ProgressView.progressTintColor = value2Actual >= value2Goal ? greenColor : yellowColor
if value2Goal == 0 {
DispatchQueue.main.async {
cell?.value2ProgressView.setProgress(Float(value2Actual), animated: true)
}
} else {
DispatchQueue.main.async {
cell?.value2ProgressView.setProgress(Float(value2Actual)/Float(value2Goal), animated: true)
}
}
let value3Goal = goalDict["value3"]!
let value3Actual = actualDict["value3"]!
cell?.value3ProgressView.progressTintColor = value3Actual >= value3Goal ? greenColor : yellowColor
if value3Goal == 0 {
DispatchQueue.main.async {
cell?.value3ProgressView.setProgress(Float(value3Actual), animated: true)
}
} else {
DispatchQueue.main.async {
cell?.value3ProgressView.setProgress(Float(value3Actual)/Float(value3Goal), animated: true)
}
}
let value4Goal = goalDict["value4"]!
let value4Actual = actualDict["value4"]!
cell?.value4ProgressView.progressTintColor = value4Actual >= value4Goal ? greenColor : yellowColor
if value4Goal == 0 {
DispatchQueue.main.async {
cell?.value4ProgressView.setProgress(Float(value4Actual), animated: true)
}
} else {
DispatchQueue.main.async {
cell?.value4ProgressView.setProgress(Float(value4Actual)/Float(value4Goal), animated: true)
}
}
let value5Goal = goalDict["value5"]!
let value5Actual = actualDict["value5"]!
cell?.value5ProgressView.progressTintColor = value5Actual >= value5Goal ? greenColor : yellowColor
if value5Goal == 0 {
DispatchQueue.main.async {
cell?.value5ProgressView.setProgress(Float(value5Actual), animated: true)
}
} else {
DispatchQueue.main.async {
cell?.value5ProgressView.setProgress(Float(value5Actual)/Float(value5Goal), animated: true)
}
}
let totalGoal = goalDict["total"] as! Int
let totalActual = actualDict["count"]!
cell?.totalProgressView.progressTintColor = totalActual >= totalGoal ? greenColor : yellowColor
if totalGoal == 0 {
DispatchQueue.main.async {
cell?.totalProgressView.setProgress(Float(totalActual), animated: true)
}
} else {
DispatchQueue.main.async {
cell?.totalProgressView.setProgress(Float(totalActual)/Float(totalGoal), animated: true)
}
}
return cell!
}
else {
//another cells are used in other sections
}
}
here myArray = [[String: [String: Int]]]
Thanks in advance.

There is one way to handle this situations, You should try setting the different identifier for the cell's but that will affect your performance. after setting this approach, you should check for the performance of scrolling on your device.
let cellIdentifier = "myIdentifier_\(indexPath.section)_\(indexPath.row)"
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
if cell == nil {
tableView.register(UINib.init(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
}
This is not the best approach, but can help avoid the repeating process issue.
Try and share results.

Related

Instantly update front end with new array value

I'm creating an app where users will send messages. I have the messenger working but the front end isn't updating instantly when a new comment is entered. You have to back out and then come back in to get it to show up.
I've researched examples and got the code from a course I took on udemy. I've tried with tableview.reload() and tableview.insertrows(). I'm sending the data using JSON to PHP and MySQL. It's showing in the db immediately but my code has it updating the UI first, it's just not working.
let messagetext = replyTxt.text.trimmingCharacters(in: .whitespacesAndNewlines)
hhmessages.insert(messagetext as AnyObject, at: hhmessages.endIndex)
let indexPath = IndexPath(row: self.hhmessages.count - 1, section: 0)
// update tableView
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
// scroll to the bottom
tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
// empty textView and reload it
replyTxt.text = ""
textViewDidChange(replyTxt)
let recipient = messages["username"] as! String
let rid = String(describing: messages["recipient_id"]!)
let uuid = messages["uuid"] as! String
puuid = UUID().uuidString
// prepare request
let url = URL(string: "http://localhost/messagepost.php")!
let body = "sender_id=\(user_id)&sender=\(username)&text=\(messagetext)&recipient_id=\(rid)&recipient=\(recipient)&uuid=\(uuid)&puuid=\(puuid)"
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = body.data(using: .utf8)
The code should instantly update the UITableView with the latest comment and then commit to the database. I've been trying for two days with no luck. Any help is welcomed.
UPDATE WITH TABLE SOURCE CODE
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hhmessages.count
}
// Return cell to display on the tableView
// How many sections. Means numberOfSections * rows = view
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let colorSmoothGray = UIColor(red: 229/255, green: 229/255, blue: 234/255, alpha: 1)
let colorBrandBlue = UIColor(red: 148 / 255, green: 33 / 255, blue: 147 / 255, alpha: 1)
let pictureURL = hhmessages[indexPath.row]["uploadpath"] as? String
// no picture in the post
if pictureURL?.isEmpty == true {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ConversationCell
cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));
isLoading = true
let hhpost = hhmessages[indexPath.row]
let smimages = hhpost["path"] as? UIImage
let text = hhmessages[indexPath.row]["messagetext"] as! String
cell.messageLbl.text = text
cell.smavaImg.image = smimages
cell.messageLbl.textAlignment = .right
cell.messageLbl.backgroundColor = colorSmoothGray
cell.messageLbl.textColor = .black
cell.messageLbl.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
cell.messageLbl.font?.withSize(25)
cell.messageLbl.clipsToBounds = true
// get main queue to this block of code to communicate back
DispatchQueue.main.async {
cell.messageLbl.sizeToFit()
tableView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi)
cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
}
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PicCell", for: indexPath) as! PicConversationCell
cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));
cell.smavaImg.image = smimages
//if let message = messageData {
for i in 0 ..< self.incoming.count {
// Confiture the constraints for cell
if self.incoming[indexPath.row] == 1 {
// Constraints
cell.lefBubbleConstraint.isActive = true
cell.rightBubbleConstraint.isActive = false
if cell.postpictureImg.image == nil {
cell.postpictureImg.backgroundColor = colorwhite
cell.postpictureImg.clipsToBounds = true
}
else {
cell.postpictureImg.backgroundColor = .clear
cell.postpictureImg.clipsToBounds = true
}
}
else if self.incoming[indexPath.row] == 0 {
// Constraints
cell.lefBubbleConstraint.isActive = false
cell.rightBubbleConstraint.isActive = true
if cell.postpictureImg.image == nil {
cell.postpictureImg.backgroundColor = colorwhite
cell.postpictureImg.clipsToBounds = true
}
else {
cell.postpictureImg.backgroundColor = .clear
cell.postpictureImg.clipsToBounds = true
}
}
// pictures logic
let pictureString = hhmessages[indexPath.row]["uploadpath"] as? String
let pictureURL = URL(string: pictureString!)!
// if there are still pictures to be loaded
if hhmessages.count != pictures.count {
URLSession(configuration: .default).dataTask(with: pictureURL) { (data, response, error) in
// downloaded
if let image = UIImage(data: data!) {
self.pictures.append(image)
DispatchQueue.main.async {
cell.postpictureImg.image = image
}
}
}.resume()
// cached picture
} else {
DispatchQueue.main.async {
cell.postpictureImg.image = self.pictures[indexPath.row]
}
}
}
return cell
}
}
Are you adding the element to what your numberOfRowsForSection calls? Before you call insertRows the data source needs to be update.

iOS UITableView is not selectable when it is scrolled

I am facing this issue and I am not getting any clear answer. I have a UITableView which works fine when it loads for the first time but when it scrolls, it is not selectable, didSelectRowAtIndexPath never gets a call. Also I've button in my cell, it's not clickable either. But I can still scroll tableview. This is my code :
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell : ContactTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ContactCell") as! ContactTableViewCell
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
cell.delegate = self
let randomNumber = Int(arc4random_uniform(UInt32(colorsArray!.count)))
if(!shouldUseFilteredResults) {
cell.setFilteredContents(_with: addressBookArray![indexPath.row], theme: themeStyle!, randomColor: colorsArray![randomNumber])
}
else {
cell.setFilteredContents(_with: filteredAddressBook[indexPath.row], theme: themeStyle!, randomColor: colorsArray![randomNumber])
cell.contactNameLabel.attributedText = self.highlightSearchResult(resultString: cell.contactNameLabel.text!)
}
return cell
}
In cell :
public func setFilteredContents (_with contact : [String : Any], theme : Theme, randomColor : UIColor)
{
contactDictionary = contact;
contactNameLabel.text = String(describing: contact["FullName"]!)
self.setTheme(theme: theme, randomColor: randomColor)
if (contact["ImageDataString"] != nil)
{
let imageData = Data(base64Encoded: contact["ImageDataString"] as! String)
let image = UIImage(data: imageData!)
let imageView = UIImageView(frame: thumbnailView.bounds)
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = CGFloat(imageView.height()/2)
imageView.image = image
thumbnailView.addSubview(imageView)
}
else {
self.setThumbnail(fullName: contactNameLabel.text!)
}
}
private func setThumbnail(fullName : String)
{
let vibrancy = UIVibrancyEffect(blurEffect: UIBlurEffect(style: .light))
let backgroundView = UIVisualEffectView(effect: vibrancy)
backgroundView.frame = thumbnailView.bounds
thumbnailView.addSubview(backgroundView)
let thumbNailLabel = UILabel(frame: thumbnailView.bounds)
thumbNailLabel.font = UIFont.systemFont(ofSize: 13)
thumbNailLabel.textColor = UIColor.white
thumbNailLabel.textAlignment = .center
var initialLettersString = String()
let wordsArray = fullName.components(separatedBy: NSCharacterSet.whitespaces)
for word in wordsArray
{
if(word != "")
{
initialLettersString.append(String(describing: word.characters.first!))
}
}
if(initialLettersString.characters.count == 1)
{
initialLettersString = String(initialLettersString[..<initialLettersString.index(initialLettersString.startIndex, offsetBy: 1)]).uppercased()
}
if(initialLettersString.characters.count > 1)
{
initialLettersString = String(initialLettersString[..<initialLettersString.index(initialLettersString.startIndex, offsetBy: 2)]).uppercased()
}
thumbNailLabel.text = initialLettersString as String
backgroundView.contentView.addSubview(thumbNailLabel)
}
private func setTheme(theme : Theme, randomColor : UIColor)
{
let isContactFavorite = contactDictionary?[Constants.TTCIsContactFavoriteKey] as? Bool
thumbnailView.backgroundColor = randomColor
if(theme == Theme.Light)
{
contactNameLabel.textColor = UIColor.black
lineView.backgroundColor = UIColor.lightGray
if(!isContactFavorite!)
{
favoritesButton.setImage(UIImage(named: "favorite_dark.png"), for: .normal)
}
else
{
favoritesButton.setImage(UIImage(named: "favorite.png"), for: .normal)
}
}
else if(theme == Theme.Dark)
{
contactNameLabel.textColor = UIColor.white
lineView.backgroundColor = UIColor.gray
if(!isContactFavorite!)
{
favoritesButton.setImage(UIImage(named: "favorite_white.png"), for: .normal)
}
else
{
favoritesButton.setImage(UIImage(named: "favorite.png"), for: .normal)
}
}
for view in thumbnailView.subviews
{
view.removeFromSuperview()
}
}
Custom Cell :
What could be the issue?
This may also because of overriding default touch events with UITapRecognizers in the View Hierarchy
Remove any tap recognizers set to to UITableview Parent View
Thanks everyone for help. Issue was with SwipeCellKit which I was using for CustomCell. Removing it solved it!
You are doing everything right.
Just check your button frame does lies inside your cell.
Some times your cell height is less and button is outside the bounds of cell.

how to prevent cell reuse of uicollectionview

I use UICollectionView and refer to this link implement scroll table view Fixed single column and row like below image.
but when I want to add shadow property, it display messy.
I think the reason is that I reuse cell when render visual view, but I don't know how to fix it :(
provide my code below, thanks for your time!
import Foundation
import SwiftyJSON
#objc(RNCollection)
class RNCollection : UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {
var contentCellIdentifier = "CellIdentifier"
var collectionView: UICollectionView!
static var dataSource = []
static var sections = 0
static var rows = 0
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: CGRectZero, collectionViewLayout: RNCollectionLayout())
self.registerClass(RNCollectionCell.self, forCellWithReuseIdentifier: contentCellIdentifier)
self.directionalLockEnabled = false
self.backgroundColor = UIColor.whiteColor()
self.delegate = self
self.dataSource = self
self.frame = frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setConfig(config: String!) {
var json: JSON = nil;
if let data = config.dataUsingEncoding(NSUTF8StringEncoding) {
json = JSON(data: data);
};
RNCollection.dataSource = json["dataSource"].arrayObject!
RNCollection.sections = json["sections"].intValue
RNCollection.rows = json["rows"].intValue
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let contentCell: RNCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier(contentCellIdentifier, forIndexPath: indexPath) as! RNCollectionCell
contentCell.textLabel.textColor = UIColor.blackColor()
if indexPath.section == 0 {
contentCell.backgroundColor = UIColor(red: 232/255.0, green: 232/255.0, blue: 232/255.0, alpha: 1.0)
if indexPath.row == 0 {
contentCell.textLabel.font = UIFont.systemFontOfSize(16)
contentCell.textLabel.text = (RNCollection.dataSource[0] as! NSArray)[0] as? String
} else {
contentCell.textLabel.font = UIFont.systemFontOfSize(14)
contentCell.textLabel.text = (RNCollection.dataSource[0] as! NSArray)[indexPath.row] as? String
}
} else {
contentCell.textLabel.font = UIFont.systemFontOfSize(12)
contentCell.backgroundColor = UIColor.whiteColor()
if(indexPath.section % 2 == 0) {
contentCell.backgroundColor = UIColor(red: 234/255.0, green: 234/255.0, blue: 236/255.0, alpha: 1.0)
}
if indexPath.row == 0 {
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[0] as? String
contentCell.layer.shadowOffset = CGSize(width: 3, height: 3)
contentCell.layer.shadowOpacity = 0.7
contentCell.layer.shadowRadius = 2
} else {
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[indexPath.row] as? String
}
}
return contentCell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return RNCollection.rows
}
// MARK - UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return RNCollection.sections
}
}
You are specifying your customizations for a certain section/row and in your else condition you just set the text. You should specify your required/default customization in your else condition. Consider this part of your code
if indexPath.row == 0 {
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[0] as? String
contentCell.layer.shadowOffset = CGSize(width: 3, height: 3)
contentCell.layer.shadowOpacity = 0.7
contentCell.layer.shadowRadius = 2
} else {
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[indexPath.row] as? String
}
You have specified your shadowOffset,shadowOpacity and shadowRadius in your if condition, but you ignored them in your else condition. If you specify the appearance in the else condition you won't have the problem you are facing now. It should look something like this
if indexPath.row == 0 {
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[0] as? String
contentCell.layer.shadowOffset = CGSize(width: 3, height: 3)
contentCell.layer.shadowOpacity = 0.7
contentCell.layer.shadowRadius = 2
} else {
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[indexPath.row] as? String
contentCell.layer.shadowOffset = 0
contentCell.layer.shadowOpacity = 0
contentCell.layer.shadowRadius = 0
}
If you reset everything properly in all your condition checks, this problem won't occur.
You set up shadow when your row is equal to 0:
if indexPath.row == 0 {...}
but if the row is not equal to zero collection view can reuse the cell when the shadow was already set. The solution is reset the shadow when row is not 0, in your code you can do something like this (see todo):
if indexPath.row == 0 { // Set up shadow here
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[0] as? String
contentCell.layer.shadowOffset = CGSize(width: 3, height: 3)
contentCell.layer.shadowOpacity = 0.7
contentCell.layer.shadowRadius = 2
} else {
contentCell.textLabel.text = (RNCollection.dataSource[indexPath.section] as! NSArray)[indexPath.row] as? String
//TODO: Remove the shadow here
}
If you wanna save the cell setting everytime when your cell appears,you should set every possible condition of the method "func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell".Do not prevent the reuse of collectionView only if your data or items is few.
In Objective-C, but in Swift, the logic should be the same and not that hard to translate.
In RNCollectionCell.
//These enums may have a better nam, I agree
typedef enum : NSUInteger {
RNCollectionCellStyleColumnTitle,
RNCollectionCellStyleColumnValue,
RNCollectionCellStyleRowTitle,
RNCollectionCellStyleUpperLeftCorner,
} RNCollectionCellStyle;
-(void)titleIt:(NSString *)title forStyle:(RNCollectionCellStyle)style forSection:(NSUInteger)section
{
self.textLabel.textColor = [UIColor blackColor]; //Since it's always the same could be done elsewhere once for all.
//Note I didn't check completely if the case were good, and I didn't add the background color logic, but it should be done here too.
switch (style)
{
case RNCollectionCellStyleColumnTitle:
{
self.textLabel.font = [UIFont systemFontOfSize:12];
self.layer.shadowOffset = CGSizeMake(3, 3);
self.layer.shadowOpacity = 0.7;
self.layer.shadowRadius = 2;
}
break;
case RNCollectionCellStyleColumnValue:
{
self.textLabel.font = [UIFont systemFontOfSize:12];
self.layer.shadowOffset = CGSizeZero;
self.layer.shadowOpacity = 0;
self.layer.shadowRadius = 0;
}
break;
case RNCollectionCellStyleRowTitle:
{
self.textLabel.font = [UIFont systemFontOfSize:14];
self.layer.shadowOffset = CGSizeZero;
self.layer.shadowOpacity = 0;
self.layer.shadowRadius = 0;
}
break;
case RNCollectionCellStyleUpperLeftCorner:
{
self.textLabel.font = [UIFont systemFontOfSize:16];
self.layer.shadowOffset = CGSizeZero;
self.layer.shadowOpacity = 0;
self.layer.shadowRadius = 0;
}
break;
default:
break;
}
self.textLabel.text = title;
}
-(void)prepareForReuse
{
[super prepareForReuse];
self.layer.shadowOffset = CGSizeZero;
self.layer.shadowOpacity = 0;
self.layer.shadowRadius = 0;
}
In RNCollection:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
RNCollectionCell *contentCell = [collectionView dequeueReusableCellWithReuseIdentifier: contentCellIdentifier forIndexPath:indexPath];
//Here check what style you need to do
//Also, you seems to always do dataSource[indexPath.section][indexPath.row], you just use the "if test values".
[contentCell titleIt:dataSource[indexPath.section][indexPath.row]
forStyle:RNCollectionCellStyleColumnTitle
forSection:[indexPath section]];
return contentCell;
}
As said, I didn't check if all the cases were correct according to your needs.
But, you don't "prevent reuse of cell", it's optimized.
You need to "reset" values before reused or when you set the values. In theory the thing done in prepareForReuse should be enough and you shouldn't have to "reset" the layer effect in the switch, but I honestly didn't check it.

Jerking motion after table view is reloaded

I'm getting a weird jerking motion whenever I reload my row and scroll up. It's fine if it's scrolling down, but scrolling back up is causing a horrible jerking effect, rendering the app useless.
I've attached the code for not only the cellforrowatindex path, but the buttons as well.
cell for row:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
cell.nopebutton.tag = indexPath.row
cell.nicebutton.tag = indexPath.row
cell.killText.text = object.valueForKey("text") as! String
cell.layoutMargins = UIEdgeInsetsZero
cell.killText.numberOfLines = 0
let score = object.valueForKey("count") as! Int
cell.count.text = "\(score)"
if cell.count.text == "\(0)"
{
cell.count.textColor = UIColor.grayColor()
}
if cell.count.text > "\(0)"
{
cell.count.textColor = UIColor(red: 42.0/255, green: 204.0/255, blue: 113.0/255, alpha: 1)
}
if cell.count.text < "\(0)"
{
cell.count.textColor = UIColor(red: 231.0/255, green: 76.0/255, blue: 50.0/255, alpha: 1)
}
if cell.count.text == "\(50)"
{
cell.count.textColor = UIColor(red: 249.0/255, green: 191.0/255, blue: 59.0/255, alpha: 1)
}
if let dict : NSDictionary = NSUserDefaults.standardUserDefaults().objectForKey("userNiceNopeDictionary") as? NSDictionary {
cell.nicebutton.enabled = true
cell.nopebutton.enabled = true
if let nice = dict[object.objectId] as? Bool{
if nice {
cell.nicebutton.enabled = false
}
else {
cell.nopebutton.enabled = false
}
}
}
let dateUpdated = object.createdAt as NSDate
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "h:mm a"
cell.time.text = (NSString(format: "%#", dateFormat.stringFromDate(dateUpdated)) as String) as String
let replycnt = object.objectForKey("replies") as! Int
if cell.count.text == "\(-10)"
{
object.deleteInBackground()
}
return cell
}
buttons
#IBAction func topButton(sender: AnyObject) {
var button = sender as! UIButton
// var view = button.superview
//
// var otherButton = view?.viewWithTag(102) as! UIButton
// var label = button.superview!.viewWithTag(110) as! UILabel
// otherButton.enabled = true
// button.enabled = false
var rowNumber = button.tag
var mutableDict = NSMutableDictionary()
if let dict = NSUserDefaults.standardUserDefaults().objectForKey("userNiceNopeDictionary") {
mutableDict = dict.mutableCopy() as! NSMutableDictionary
}
let obj = self.objects[rowNumber] as! PFObject
mutableDict.setValue(true, forKey: obj.objectId)
NSUserDefaults.standardUserDefaults().setObject(mutableDict, forKey: "userNiceNopeDictionary")
NSUserDefaults.standardUserDefaults().synchronize()
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = objectAtIndexPath(hitIndex)
object.incrementKey("count")
//label.text = object.objectForKey("count") as! String
// self.tableView.reloadData()
object.save()
self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.None)
}
#IBAction func bottomButton(sender: AnyObject) {
var button = sender as! UIButton
var rowNumber = button.tag
var mutableDict = NSMutableDictionary()
if let dict = NSUserDefaults.standardUserDefaults().objectForKey("userNiceNopeDictionary") {
mutableDict = dict.mutableCopy() as! NSMutableDictionary
}
let obj = self.objects[rowNumber] as! PFObject
mutableDict.setValue(false, forKey: obj.objectId)
NSUserDefaults.standardUserDefaults().setObject(mutableDict, forKey: "userNiceNopeDictionary")
NSUserDefaults.standardUserDefaults().synchronize()
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = objectAtIndexPath(hitIndex)
object.incrementKey("count", byAmount: -1)
self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.None)
object.save()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "killDetail"){
let indexPath = self.tableView.indexPathForSelectedRow()
let obj = self.objects[indexPath!.row] as! PFObject
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.kill = obj
}
}
}
I'm under the impression that there's an issue in my heights so I've included that code as well.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.separatorInset = UIEdgeInsetsZero
self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
self.tableView.separatorColor = UIColor(red: 236.0/255, green: 240.0/255, blue: 241.0/255, alpha: 1)
navigationController?.tabBarController?.tabBar.hidden = true
tabBarController?.tabBar.hidden = true
self.tableView.estimatedRowHeight = 60
self.tableView.rowHeight = UITableViewAutomaticDimension
locationManager.desiredAccuracy = 1000
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
I can't for the life of me figure out what's going on causing this jerk. Like I said I think it might have to do with the automaticdimension in the row height, but when I change that the issue is still apparent.
Don't read that dictionary from user defaults every time you dequeue a cell. Do it once, outside of tableView(cellForRowAtIndexPath:. Do it in viewDidLoad or something. Picking out an element from the dictionary once it's in memory is fine though... You just don't want to read from disk (or flash, I guess) every time a cell is dequeued.
If the dictionary is being changed, for any reason, in other places and you want to maintain a consistent state, read the dictionary in once and just pass it around between controllers/view controllers (through, e.g. prepareForSegue) and write it to user defaults when it's changed.
The date formatter is probably not that big or complex of an object to make, but you only need to make one once, and then can reuse it, so I suggest you only create one once too.
edit: If you have a problem just scrolling up, it could be that the estimated row height doesn't match the actual row height. Try removing the estimated height.

Vote system using double tap

I am trying to implement a vote system when users double tap the image. When the user double taps the image the vote count adds one and saves to parse. In my cell I have
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
//postsLabel.textAlignment = NSTextAlignment.Center
bottomBlurView.backgroundColor = UIColor(red: 0, green: 0.698, blue: 0.792, alpha: 0.75)
votesLabel!.textAlignment = NSTextAlignment.Right
let gesture = UITapGestureRecognizer(target: self, action: Selector("onDoubleTap:"))
gesture.numberOfTapsRequired = 2
contentView.addGestureRecognizer(gesture)
heartIcon?.hidden = true
//profileImageView.layer.cornerRadius = profileImageView.frame.height / 2
}
func onDoubleTap (sender: AnyObject) {
if self.complition != nil
{
self.complition!()
}
heartIcon?.hidden = false
heartIcon?.alpha = 1.0
UIView.animateWithDuration(1.0, delay:1.0, options:nil, animations: {
self.heartIcon?.alpha = 0
}, completion: {
(value:Bool) in
self.heartIcon?.hidden = true
})
}
}
and in my collection view controller I have
func onDoubleTap (indexPath:NSIndexPath)
{
let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as! NewCollectionViewCell
let object = self.votes[indexPath.row]
if let likes = object["votes"] as? Int
{
object["votes"] = likes + 1
object.saveInBackgroundWithBlock{ (success:Bool,error:NSError?) -> Void in
println("Data saved")
}
cell.votesLabel?.text = "\(likes + 1)"
}
else
{
object["votes"] = 1
object.saveInBackgroundWithBlock{ (success:Bool,error:NSError?) -> Void in
println("Data saved")
}
cell.votesLabel?.text = "1"
}
}
However this does not add one and does not save to parse. Any help is appreciated. Thank you
You should place the first onDoubleTap in your collectionView's cellForItemAtIndexPath and configure it there.

Resources