• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:44:34 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我快速创建了一个自定义 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-推荐答案


您使用的是系统类型按钮吗?

为了摆脱这种奇怪的动画,请尝试在设置按钮标题之前设置 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap