菜鸟教程小白 发表于 2022-12-13 08:44:34

ios - Swift UICollectionView Cells 移动/UIButton 标签在 ReloadData 上闪烁


                                            <p><p>我快速创建了一个自定义 UICollectionViewCell,并在 UIButton 标题上看到了一些奇怪的行为。下面是我的一些代码,还显示了该行为的视频。
我已经用单元格背景颜色等进行了一些其他测试,当我调用 collectionView.ReloadData 时,似乎整个单元格都在移动。 UILabel 文本显示正确,但 UIButton 标题更新较慢。</p>

<p>视频:<a href="http://youtu.be/82kwwdaeNbw" rel="noreferrer noopener nofollow">http://youtu.be/82kwwdaeNbw</a> </p>

<p>代码(部分):</p>

<h2>FirstViewController.swift</h2>

<pre><code>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(&#34;updateAll&#34;), userInfo: nil, repeats: true)
}

func updateAll() {
    //      getFetchedResultController()
    //      updateStatus()
    //      updateImage()
    cView.reloadData()
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
    var returnCell: customCell!
    let object = fetchedResultsController.sections!.objects! as! ControlDevice

    if object.type == nil {
      object.type = &#34;&#34;
    }
    if let type = object.type {
      switch type {

      case &#34;Button&#34;:    // Returns a Button Cell
            let cellB = collectionView.dequeueReusableCellWithReuseIdentifier(&#34;Cell_Button&#34;, 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(&#34;GRsegueToEdit:&#34;))
            doubleTapGR.numberOfTapsRequired = 2
            cellB.addGestureRecognizer(doubleTapGR)

            returnCell = cellB

      case &#34;Image&#34;:
            let cellI = collectionView.dequeueReusableCellWithReuseIdentifier(&#34;Cell_Image&#34;, 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(&#34;GRsegueToEdit:&#34;))
            doubleTapGR.numberOfTapsRequired = 2

            cellI.addGestureRecognizer(doubleTapGR)

            returnCell = cellI

      default:    // Returns a Text Cell
            let cellD = collectionView.dequeueReusableCellWithReuseIdentifier(&#34;Cell_Text&#34;, 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(&#34;GRsegueToEdit:&#34;))
            doubleTapGR.numberOfTapsRequired = 2

            cellD.addGestureRecognizer(doubleTapGR)

            returnCell = cellD
      }
    }
    return returnCell
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
    return fetchedResultsController.sections!.numberOfObjects
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -&gt; Int {
    return (fetchedResultsController.sections!.count)
}
</code></pre>

<h2>CustomCell.swift</h2>

<pre><code>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!
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您使用的是系统类型按钮吗?</p>

<p>为了摆脱这种奇怪的动画,请尝试在设置按钮标题之前设置 button.titleLabel.text = title。</p>

<pre><code>self.button.titleLabel.text = @&#34;Hello!&#34;;
;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift UICollectionView Cells 移动/UIButton 标签在 ReloadData 上闪烁,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31102237/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31102237/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift UICollectionView Cells 移动/UIButton 标签在 ReloadData 上闪烁