我有一个 Collection View ,它将有一个 UILabel 和 2-5 个 UIButton。
我希望根据每个单元格显示多少个按钮来调整单元格的大小。我知道每个按钮的高度大约为 100。
class myViewController: UIViewController {
var myCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 100, right: 0) // add spacing to the bottom
layout.itemSize = CGSize(width: self.view.frame.width, height: 300)
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 20
layout.minimumInteritemSpacing = 20
myCollectionView=UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), collectionViewLayout: layout)
myCollectionView.delegate=self
myCollectionView.dataSource=self
myCollectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
myCollectionView.alwaysBounceVertical = false
myCollectionView.showsVerticalScrollIndicator = false
myCollectionView.translatesAutoresizingMaskIntoConstraints=false
myCollectionView.backgroundColor=UIColor.white
myCollectionView.isPagingEnabled = false
loadViews()
}
func loadViews() {
self.view.addSubview(myCollectionView)
myCollectionView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive=true
myCollectionView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive=true
myCollectionView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive=true
myCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive=true
}
}
注意上面的代码块有 layout.itemSize = CGSize(width: self.view.frame.width, height: 300) 这对 3 个按钮很有效 (3*100 = 300) .
然后在设置我的单元格类时,我创建了我的按钮,然后根据变量(在底部)确定它们的可见性。
class MyCollectionViewCell: UICollectionViewCell {
var btn1: UIButton!
var btn2: UIButton!
var btn3: UIButton!
var btn4: UIButton!
var btn5: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
addSubview(lblQue)
lblQue.topAnchor.constraint(equalTo: self.topAnchor, constant: 30).isActive=true
lblQue.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 12).isActive=true
lblQue.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -12).isActive=true
lblQue.heightAnchor.constraint(equalToConstant: 50).isActive=true
let btnWidth: CGFloat = 650
let btnHeight: CGFloat = 65
btn1 = getButton(tag: 0)
addSubview(btn1)
NSLayoutConstraint.activate([btn1.topAnchor.constraint(equalTo: lblQue.bottomAnchor, constant: 10), btn1.leftAnchor.constraint(equalTo: self.centerXAnchor, constant: -300), btn1.widthAnchor.constraint(equalToConstant: btnWidth), btn1.heightAnchor.constraint(equalToConstant: btnHeight)])
btn1.addTarget(self, action: #selector(btnOptionAction), for: .touchUpInside)
btn2 = getButton(tag: 1)
addSubview(btn2)
NSLayoutConstraint.activate([btn2.topAnchor.constraint(equalTo: btn1.bottomAnchor, constant: 10), btn2.leftAnchor.constraint(equalTo: self.centerXAnchor, constant: -300), btn2.widthAnchor.constraint(equalToConstant: btnWidth), btn2.heightAnchor.constraint(equalToConstant: btnHeight)])
btn2.addTarget(self, action: #selector(btnOptionAction), for: .touchUpInside)
btn3 = getButton(tag: 2)
addSubview(btn3)
NSLayoutConstraint.activate([btn3.topAnchor.constraint(equalTo: btn2.bottomAnchor, constant: 10), btn3.leftAnchor.constraint(equalTo: self.centerXAnchor, constant: -300), btn3.widthAnchor.constraint(equalToConstant: btnWidth), btn3.heightAnchor.constraint(equalToConstant: btnHeight)])
btn3.addTarget(self, action: #selector(btnOptionAction), for: .touchUpInside)
btn4 = getButton(tag: 3)
addSubview(btn4)
NSLayoutConstraint.activate([btn4.topAnchor.constraint(equalTo: btn3.bottomAnchor, constant: 10), btn4.leftAnchor.constraint(equalTo: self.centerXAnchor, constant: -300), btn4.widthAnchor.constraint(equalToConstant: btnWidth), btn4.heightAnchor.constraint(equalToConstant: btnHeight)])
btn4.addTarget(self, action: #selector(btnOptionAction), for: .touchUpInside)
btn5 = getButton(tag: 4)
addSubview(btn5)
NSLayoutConstraint.activate([btn5.topAnchor.constraint(equalTo: btn4.bottomAnchor, constant: 10), btn5.leftAnchor.constraint(equalTo: self.centerXAnchor, constant: -300), btn5.widthAnchor.constraint(equalToConstant: btnWidth), btn5.heightAnchor.constraint(equalToConstant: btnHeight)])
btn5.addTarget(self, action: #selector(btnOptionAction), for: .touchUpInside)
}
func getButton(tag: Int) -> UIButton {
let btn=UIButton()
btn.tag=tag
btn.setTitle("Option", for: .normal)
btn.setTitleColor(UIColor.black, for: .normal)
btn.backgroundColor=UIColor.white
btn.layer.borderWidth=1
btn.layer.borderColor=UIColor.darkGray.cgColor
btn.layer.cornerRadius=5
btn.clipsToBounds=true
btn.translatesAutoresizingMaskIntoConstraints=false
return btn
}
let lblQue: UILabel = {
let lbl=UILabel()
lbl.text="This is a question and you have to answer it?"
lbl.textColor=UIColor.black
lbl.textAlignment = .center
lbl.font = UIFont.systemFont(ofSize: 20)
lbl.numberOfLines=4
lbl.translatesAutoresizingMaskIntoConstraints=false
return lbl
}()
var myVariable: MyClassIMade? {
didSet {
// go through and determine button Text and Visibility of each button
// i.e.
// if 1>0 {
// btn3.visible = false
// } else {
// btn3.visible = true
// }
}
}
那么我如何确定有多少按钮可见,以确定每个部分的单元格大小?
最佳答案
这样的事情怎么样?只需将所有按钮放在一个数组中,然后遍历它们以检查哪些按钮被隐藏,哪些没有。你可以使用
.isHidden 隐藏和显示它们,然后检查哪个是隐藏的并返回数字..
示例函数:
func getButtonsCount(buttons: [UIButton]) -> Int{
//A counter to get how many button are not hidden
var count = Int()
//A Loop to check which buttons are hidden and increment the counter
for button in buttons {
if button.isHidden == true {
count += 1
}
}
return count
}
关于ios - 根据按钮数量设置 CollectionView 单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56945371/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
这里有一个很好的答案解释了如何在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”结果的
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c