jjzjj

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

coder 2024-01-24 原文

我在 swift 中创建了自定义 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)
}

自定义单元格.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!
}

最佳答案

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

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

有关ios - Swift UICollectionView Cells 移动/UIButton 标签在 ReloadData 上闪烁的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby - 在院子里用@param 标签警告 - 2

    我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. css - 用 watir 检查标签类? - 2

    我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes

  7. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  8. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  9. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  10. ruby - 如何用 Nokogiri 解析连续的标签? - 2

    我有这样的HTML代码:Label1Value1Label2Value2...我的代码不起作用。doc.css("first").eachdo|item|label=item.css("dt")value=item.css("dd")end显示所有首先标记,然后标记标签,我需要“标签:值” 最佳答案 首先,您的HTML应该有和中的元素:Label1Value1Label2Value2...但这不会改变您解析它的方式。你想找到s并遍历它们,然后在每个你可以使用next_element得到;像这样:doc=Nokogiri::HTML(

随机推荐