我快速创建了一个自定义 UICollectionViewCell,并在 UIButton 标题上看到了一些奇怪的行为。下面是我的一些代码,还显示了该行为的视频。
我已经用单元格背景颜色等进行了一些其他测试,当我调用 collectionView.ReloadData 时,似乎整个单元格都在移动。 UILabel 文本显示正确,但 UIButton 标题更新较慢。
视频:http://youtu.be/82kwwdaeNbw
代码(部分):
FirstViewController.swift
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)
}
CustomCell.swift
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!
}
Best Answer-推荐答案 strong>
您使用的是系统类型按钮吗?
为了摆脱这种奇怪的动画,请尝试在设置按钮标题之前设置 button.titleLabel.text = title。
self.button.titleLabel.text = @"Hello!";
[self.button setTitle"Hello!" forState:UIControlStateNormal];
关于ios - Swift UICollectionView Cells 移动/UIButton 标签在 ReloadData 上闪烁,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/31102237/
|