Ios 如何使用Swift在UICollectionView中显示/隐藏单元格?
我已经使用UICollectionView显示了单元格(0到9和Ok,取消按钮) 以下是我想要的:Ios 如何使用Swift在UICollectionView中显示/隐藏单元格?,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我已经使用UICollectionView显示了单元格(0到9和Ok,取消按钮) 以下是我想要的: “确定”和“取消”按钮将首先隐藏 当用户选择至少一个数字时,“取消”按钮变为可见 当用户选择total four number时,Ok按钮也将可见 下面是我完成的代码: var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "Cancel","0", "OK"] ... func collectionView(collection
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "Cancel","0", "OK"]
...
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCollectionViewCell
cell.lblNumber!.text = self.items[indexPath.item]
if (self.items[indexPath.item])=="Cancel" {
cell.hidden = true;
}
if (self.items[indexPath.item])=="OK" {
cell.hidden = true;
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.item) and value : \(self.items[indexPath.item]) count : \(counter)")
...
}
如何做到这一点?嗨,下面是一个例子:
import UIKit
class ViewController: UIViewController {
var objectNumCollectionViewCell : NumCollectionViewCell = NumCollectionViewCell()
@IBOutlet weak var lblnumber: UILabel!
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "Cancel","0", "OK"]
var strnum: String = ""
@IBOutlet weak var collectionviewMain: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - CollectionView DataSource Method
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return items.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
objectNumCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! NumCollectionViewCell
objectNumCollectionViewCell.lblNum.text = items[indexPath.item] as String
if indexPath.item == 9 {
if lblnumber.text?.characters.count > 0 {
objectNumCollectionViewCell.hidden = false
}
else{
objectNumCollectionViewCell.hidden = true
}
}
else
{
objectNumCollectionViewCell.hidden = false
}
if indexPath.item == 11 {
if strnum.characters.count > 3 {
objectNumCollectionViewCell.hidden = false
}
else{
objectNumCollectionViewCell.hidden = true
}
}
objectNumCollectionViewCell.layer.borderWidth = 1.0
objectNumCollectionViewCell.layer.borderColor = UIColor.darkGrayColor().CGColor
objectNumCollectionViewCell.layer.cornerRadius = 10.0
objectNumCollectionViewCell.layer.masksToBounds = true
return objectNumCollectionViewCell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
return UIEdgeInsetsMake(0, 5, 0, 5);
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return CGSizeMake(self.view.frame.size.width/3-10, 100)
}
// MARK: - CollectionView Delegate Method
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
objectNumCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! NumCollectionViewCell
if indexPath.item == 9 {
strnum.removeAtIndex(strnum.endIndex.predecessor())
}
else if indexPath.item == 11{
let alert:UIAlertView = UIAlertView(title: "Number Demo", message: "You have Pressed Ok", delegate: nil, cancelButtonTitle: "ok")
dispatch_async(dispatch_get_main_queue(), {
alert.show()
})
}
else
{
if strnum.characters.count < 4 {
strnum.append(Character(items[indexPath.item] as String))
}
}
lblnumber.text = strnum
collectionviewMain.reloadData()
}
}
// Custom cell class
// identifier = "cell"
import UIKit
class NumCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var lblNum: UILabel! // please declare label in storyboard
}
导入UIKit
类ViewController:UIViewController{
var objectNumCollectionViewCell:NumCollectionViewCell=NumCollectionViewCell()
@IBL弱变量lblnumber:UILabel!
风险值项目=[“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“取消”、“0”、“确定”]
var strnum:String=“”
@IBOutlet弱var collectionviewMain:UICollectionView!
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//MARK:-CollectionView数据源方法
func numberOfSectionsInCollectionView(collectionView:UICollectionView)->Int{
返回1
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回项目。计数
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{
objectNumCollectionViewCell=collectionView.dequeueReusableCellWithReuseIdentifier(“cell”,forIndexPath:indexPath)作为!NumCollectionViewCell
objectNumCollectionViewCell.lblNum.text=items[indexPath.item]作为字符串
如果indexath.item==9{
如果lblnumber.text?.characters.count>0{
objectNumCollectionViewCell.hidden=false
}
否则{
objectNumCollectionViewCell.hidden=true
}
}
其他的
{
objectNumCollectionViewCell.hidden=false
}
如果indexath.item==11{
如果strnum.characters.count>3{
objectNumCollectionViewCell.hidden=false
}
否则{
objectNumCollectionViewCell.hidden=true
}
}
objectNumCollectionViewCell.layer.borderWidth=1.0
objectNumCollectionViewCell.layer.borderColor=UIColor.darkGrayColor().CGColor
objectNumCollectionViewCell.layer.cornerRadius=10.0
objectNumCollectionViewCell.layer.masksToBounds=true
返回objectNumCollectionViewCell
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,InsertForSectionAtIndex节:Int)->UIEdgeInsets{
返回UIEdgeInsetsMake(0,5,0,5);
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,SizeFormiteIndeXPath indexPath:NSIndexPath)->CGSize{
返回CGSizeMake(self.view.frame.size.width/3-10100)
}
//标记:-CollectionView委托方法
func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath){
objectNumCollectionViewCell=collectionView.dequeueReusableCellWithReuseIdentifier(“cell”,forIndexPath:indexPath)作为!NumCollectionViewCell
如果indexath.item==9{
strnum.removeAtIndex(strnum.endIndex.preference())
}
如果indexath.item==11,则为else{
让alert:UIAlertView=UIAlertView(标题:“数字演示”,消息:“您已按下Ok”,代表:无,取消按钮提示:“Ok”)
dispatch\u async(dispatch\u get\u main\u queue(){
alert.show()
})
}
其他的
{
如果strnum.characters.count小于4{
追加(字符(项[indepath.item]作为字符串))
}
}
lblnumber.text=strnum
collectionviewMain.reloadData()
}
}
//自定义单元类
//标识符=“单元格”
导入UIKit
类NumCollectionViewCell:UICollectionViewCell{
@IBOutlet弱变量lblNum:UILabel!//请在故事板中声明标签
}
保存对cellForItemAtIndexPath方法中的Cancel&OK按钮单元格的引用。您可以先将其“contentView”设置为隐藏。然后,根据所选项目的数量,在didSelectItemAtIndexPath中,您可以使用已保存单元格的引用来隐藏/取消隐藏OK/Cancel按钮的contentView。另一种方法是保留选择列表(在didSelect..
)并强制更新(通过reloadData()
)这将根据列表的计数启用/禁用取消/确定。顺便说一下,imo启用/禁用按钮对用户来说比显示/隐藏更直观。。。
if "OK" & "CANCEL" both buttons are hidden first , you can use the textfield in which the text is printed by pressing any button .
if textfield.text == "" {
okbutton.ishidden == true
cancel button.ishdden == true
}else if textfield.text != "" {
okbutton.ishidden == false
cancel button.ishdden == false
}