简体   繁体   English

在集合视图中显示/隐藏顶部行

[英]show/hide top rows in collection view

I am trying to build login/register page using collection view I want to be able to remove the top 4 rows and shrink the collection view when the user selects the login segment control and i want to re-add these 4 rows and re-size the collection view accordingly. 我正在尝试使用集合视图构建登录/注册页面,我希望能够删除前4行并在用户选择登录段控件时缩小集合视图,并且我想重新添加这4行并重新设置大小相应的集合视图。 and i want to also be able to maintain the text the user entered in the email textfield when adding-removing these rows. 并且我还希望能够在添加-删除这些行时保留用户在电子邮件文本字段中输入的文本。 Main Screen 主屏幕

i tried to use the delete items at index path, but couldn't figure out how to re enter them at the top So I tried to use the .ishidden = true instead of deleting but changing the segment control value repeatedly causes the text entered inside the textfield to be placed within different cells. 我尝试在索引路径处使用删除项,但无法弄清楚如何在顶部重新输入它们,因此我尝试使用.ishidden = true而不是删除,但反复更改段控制值会导致在内部输入文本要放置在不同单元格中的文本字段。 For example if i am on login page with the hidden cells, and i type in Hello in email text field, then i click on Register, the full form is shown, and Hello is the text at Last Name textfield and it keeps appearing within different textfield if i keep on switching between login and register. 例如,如果我在具有隐藏单元格的登录页面上,并且在电子邮件文本字段中输入Hello,则单击“注册”,将显示完整表单,并且Hello是Last Name文本字段中的文本,并且该文本始终显示在不同的位置textfield,如果我继续在登录和注册之间切换。

The other issue i'm facing, is when i try to make the login screen to look like below Login Screen by changing the collectionView height constraint constant, and setting a minus value for collectionView.contentInset.Top, it does appear as i want it to be, but once i select the Register, it doesn't load the full form and this is how it looks like. 我面临的另一个问题是,当我尝试通过更改collectionView高度约束常量,并为collectionView.contentInset.Top设置一个负值,使登录屏幕看起来像登录屏幕下方时,确实出现了我想要的样子是的,但是一旦我选择了Register,它就不会加载完整的表单,这就是它的样子。 partial register 部分寄存器

My Code for the segment control change is as below: 我的段控制更改代码如下:

@objc func handleLoginRegisterChange() {
        if LoginRegisterSegmentControl.selectedSegmentIndex == 0 {
            LoginRegisterButton.setTitle("Login", for: .normal)
            //collectionView.reloadData()
            collectionView.cellForItem(at: IndexPath(item: 0, section: 0))?.isHidden = true
            collectionView.cellForItem(at: IndexPath(item: 1, section: 0))?.isHidden = true
            collectionView.cellForItem(at: IndexPath(item: 2, section: 0))?.isHidden = true
            collectionView.cellForItem(at: IndexPath(item: 3, section: 0))?.isHidden = true
            collectionView.contentInset.top = -160.6
            collectionViewHeight.constant = 80
        }
        else if LoginRegisterSegmentControl.selectedSegmentIndex == 1 {
            LoginRegisterButton.setTitle("Register", for: .normal)
            collectionView.cellForItem(at: IndexPath(item: 0, section: 0))?.isHidden = false
            collectionView.cellForItem(at: IndexPath(item: 1, section: 0))?.isHidden = false
            collectionView.cellForItem(at: IndexPath(item: 2, section: 0))?.isHidden = false
            collectionView.cellForItem(at: IndexPath(item: 3, section: 0))?.isHidden = false
            collectionView.reloadData()
            collectionView.contentInset.top = 0
            collectionViewHeight.constant = 240
        }
    }

My customCell only have a Textfield 我的customCell只有一个Textfield

this is how i setup the placeholders 这就是我设置占位符的方式

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: customeCellIdentifier, for: indexPath) as! CustomCell
    customCell.textField.placeholder = signUpPlaceHolders[indexPath.row]
    return customCell
}

and the signUpPlaceHolder array is : 并且signUpPlaceHolder数组是:

let signUpPlaceHolders = ["First Name", "Last Name", "Company Name", "Phone", "Email", "Password"]

i am a beginner at Swift and trying to figure out collection Views, your help will be highly appreciated. 我是Swift的初学者,试图弄清楚View的集合,将非常感谢您的帮助。

I suggest to check the UICollectionView class in Apple doc, then for your example in your segmented control action you could just update your array after declaring it as var: 我建议检查Apple文档中的UICollectionView类,然后对于分段控件操作中的示例,您可以在将数组声明为var后更新数组:

@objc func handleLoginRegisterChange() {
    if LoginRegisterSegmentControl.selectedSegmentIndex == 0 {    
        signUpPlaceHolders = ["Email", "Password"]
        collectionViewHeight.constant = 80
    } else { 
        signUpPlaceHolders = ["First Name", "Last Name", "Company Name", "Phone", "Email", "Password"]
        collectionViewHeight.constant = 240
    }
    collectionView.reloadData()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM