Reputation: 31
I've created a custom UICollectionViewCell in swift and seeing some weird behaviour on the UIButton titles. Some of my code is below, a video of the behaviour is also shown. I've done some other tests with cell background colours etc and it seems like the entire cell moves when I do a collectionView.ReloadData call. The UILabel text is shown correctly, but the UIButton title update is slower.
Video: http://youtu.be/82kwwdaeNbw
Code (partial):
class FirstViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, NSFetchedResultsControllerDelegate {
let cds = CoreDataStore()
@IBOutlet var cView: UICollectionView!
var defaultCellHeight = 40
var selectedDevice: ControlDevice?
var refreshTimer = NSTimer()
var fetchedResultsController: NSFetchedResultsController = NSFetchedResultsController()
// MARK: - Initial Setup
override func viewDidLoad() {
super.viewDidLoad()
cView.delegate = self
cView.dataSource = self
getFetchedResultController()
refreshTimer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: Selector("updateAll"), userInfo: nil, repeats: true)
}
func updateAll() {
// getFetchedResultController()
// updateStatus()
// updateImage()
cView.reloadData()
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var returnCell: customCell!
let object = fetchedResultsController.sections![indexPath.section].objects![indexPath.item] as! ControlDevice
if object.type == nil {
object.type = ""
}
if let type = object.type {
switch type {
case "Button": // Returns a Button Cell
let cellB = collectionView.dequeueReusableCellWithReuseIdentifier("Cell_Button", forIndexPath: indexPath) as! customCell_Button
cellB.CDID = object.objectID
cellB.nameButton.setTitle(object.name, forState: .Normal)
cellB.layer.borderColor = UIColor.lightGrayColor().CGColor
cellB.layer.borderWidth = 1
cellB.layer.cornerRadius = 10
let doubleTapGR = UITapGestureRecognizer(target: self, action: Selector("GRsegueToEdit:"))
doubleTapGR.numberOfTapsRequired = 2
cellB.addGestureRecognizer(doubleTapGR)
returnCell = cellB
case "Image":
let cellI = collectionView.dequeueReusableCellWithReuseIdentifier("Cell_Image", forIndexPath: indexPath) as! customCell_Image
cellI.CDID = object.objectID
cellI.nameLabel.text = object.name
if let imageData = object.lastImage {
cellI.imageView.image = UIImage(data: imageData)
}
let doubleTapGR = UITapGestureRecognizer(target: self, action: Selector("GRsegueToEdit:"))
doubleTapGR.numberOfTapsRequired = 2
cellI.addGestureRecognizer(doubleTapGR)
returnCell = cellI
default: // Returns a Text Cell
let cellD = collectionView.dequeueReusableCellWithReuseIdentifier("Cell_Text", forIndexPath: indexPath) as! customCell_Text
cellD.CDID = object.objectID
cellD.nameButton.setTitle(object.name, forState: .Normal)
cellD.nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
cellD.layer.borderColor = UIColor.lightGrayColor().CGColor
cellD.layer.borderWidth = 1
cellD.layer.cornerRadius = 10
cellD.statusLabel.text = object.lastStatus
let doubleTapGR = UITapGestureRecognizer(target: self, action: Selector("GRsegueToEdit:"))
doubleTapGR.numberOfTapsRequired = 2
cellD.addGestureRecognizer(doubleTapGR)
returnCell = cellD
}
}
return returnCell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return fetchedResultsController.sections![section].numberOfObjects
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return (fetchedResultsController.sections!.count)
}
import UIKit
import CoreData
class customCell: UICollectionViewCell {
var width: Int!
var height: Int!
var CDID: NSManagedObjectID!
}
class customCell_Button: customCell {
@IBOutlet var nameButton: UIButton!
@IBOutlet var nameLabel: UILabel!
}
class customCell_Text: customCell {
@IBOutlet var nameButton: UIButton!
@IBOutlet var nameLabel: UILabel!
@IBOutlet weak var statusLabel: UILabel!
}
class customCell_Image: customCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet var imageView: UIImageView!
}
class customCell_SectionHeader: customCell {
@IBOutlet var nameLabel: UILabel!
}
class customTableCell: UITableViewCell {
var CDID: NSManagedObjectID!
}
class customTableCell_Login: customTableCell {
@IBOutlet var nameText: UITextField!
@IBOutlet var usernameText: UITextField!
@IBOutlet var passwordText: UITextField!
@IBOutlet var deleteButton: UIButton!
}
Upvotes: 3
Views: 1013
Reputation: 101
Are you using System type buttons?
In order to get rid of this weird animation try to set button.titleLabel.text = title before setting the button's title.
self.button.titleLabel.text = @"Hello!";
[self.button setTitle:@"Hello!" forState:UIControlStateNormal];
Upvotes: 4