在下面的最小示例中,我创建了一个包含五个 UICollectionViewCell 的 UICollectionView。对于每一个,我创建一个具有相同 frame 的 CALayer 并设置其 backgroundColor 属性并将其作为子层添加到 UICollectionViewCell 的 layer 属性。最初在屏幕上的单元格按预期设置,但其余单元格的颜色可能不正确,并且在滚动时可能会在完全离开屏幕之前消失。这个问题 [1] 表明发生这种情况是因为单元格最初不在屏幕上 (?),但我从答案中看不出如何解决该问题。
下面是一个最小的工作示例。我尝试过的一些事情被注释掉了:
setNeedsDisplay()(无效)。import Foundation
import UIKit
enum Colors: Int {
case Red
case Orange
case Yellow
case Green
case Blue
}
class TestViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var collectionView = UICollectionView(frame: CGRect(x: 100, y: 100, width: 100, height: 100), collectionViewLayout: UICollectionViewFlowLayout())
let reuseIdentifier = "ColorCell"
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "ColorCell")
self.view.addSubview(self.collectionView)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
var l = CALayer()
l.frame = cell.frame
l.delegate = self
if let color = Colors(rawValue: indexPath.item) {
switch color {
case .Red:
l.backgroundColor = UIColor.redColor().CGColor
// cell.backgroundColor = UIColor.redColor()
case .Orange:
l.backgroundColor = UIColor.orangeColor().CGColor
// cell.backgroundColor = UIColor.orangeColor()
case .Yellow:
l.backgroundColor = UIColor.yellowColor().CGColor
// cell.backgroundColor = UIColor.yellowColor()
case .Green:
l.backgroundColor = UIColor.greenColor().CGColor
// cell.backgroundColor = UIColor.greenColor()
case .Blue:
l.backgroundColor = UIColor.blueColor().CGColor
// cell.backgroundColor = UIColor.blueColor()
}
} else {
l.backgroundColor = UIColor.blackColor().CGColor
// cell.backgroundColor = UIColor.redColor()
}
// for sub in cell.layer.sublayers {
// sub.removeFromSuperlayer()
// }
cell.layer.addSublayer(l)
// cell.setNeedsDisplay()
return cell
}
}
最佳答案
主要问题是您应该将层的框架设置为单元格的边界,而不是它的框架。但是,另一个问题是,当您滚动和重复使用单元格时,您将添加额外的层,因为每次调用 cellForItemAtIndexPath 时都会添加一个子层。为了解决这些问题,我将创建一个 UICollectionViewCell 的子类,并在其中添加图层并调整其大小,
class CustomCollectionViewCell: UICollectionViewCell {
var l = CALayer()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
l.frame = self.bounds
layer.addSublayer(l)
}
}
然后,cellForItemAtIndexPath 变为,
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifier, forIndexPath: indexPath) as CustomCollectionViewCell
cell.l.delegate = self
if let color = Colors(rawValue: indexPath.item) {
switch color {
case .Red:
cell.l.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.5).CGColor
case .Orange:
cell.l.backgroundColor = UIColor.orangeColor().CGColor
case .Yellow:
cell.l.backgroundColor = UIColor.yellowColor().CGColor
case .Green:
cell.l.backgroundColor = UIColor.greenColor().CGColor
case .Blue:
cell.l.backgroundColor = UIColor.blueColor().CGColor
}
} else {
cell.l.backgroundColor = UIColor.blackColor().CGColor
}
return cell
}
如果您这样做,请务必在 viewDidLoad 中注册您的自定义类而不是 UICollectionViewCell。
关于ios - 将子层添加到 UICollectionViewCell 时出现意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29331474/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的