gpt4 book ai didi

ios - 如何在 Swift 2.0 中使用标签将 TextField 添加到 Cell

转载 作者:行者123 更新时间:2023-11-30 13:54:07 25 4
gpt4 key购买 nike

我希望表格的第一行有一个带有 TextField 的单元格(在我的例子中名为 FindTextField)。我实现的方式(参见下面的代码),当单元格被重用时,如果单元格列表向下滚动,则将使用 TextField 分配更多单元格。我想知道如何使用标签或其他任何允许强制只有第一行具有 TextField 的内容,即使用户一直向下然后向上滚动也是如此。

现在,这是代码:

 func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

let cell = tableView.dequeueReusableCellWithIdentifier("identifier",
forIndexPath: indexPath) as UITableViewCell

if indexPath.row == 0 {
FindTextField.frame = CGRectMake(74, 4, cell.bounds.width - 78 , cell.bounds.height - 8)
FindTextField.hidden = false
FindTextField.placeholder = "Find"
FindTextField.font = UIFont.systemFontOfSize(20)
FindTextField.borderStyle = UITextBorderStyle.RoundedRect
FindTextField.autocorrectionType = UITextAutocorrectionType.No
FindTextField.keyboardType = UIKeyboardType.Default
FindTextField.returnKeyType = UIReturnKeyType.Done
FindTextField.clearButtonMode = UITextFieldViewMode.WhileEditing;
FindTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
FindTextField.delegate = self
FindTextField.autocapitalizationType = .AllCharacters
cell.contentView.addSubview(FindTextField)
cell.imageView?.image = UIImage(named: "menu_find.png")
cell.textLabel?.hidden = true
}

cell.textLabel!.text = StationsChatList[indexPath.row]
cell.textLabel!.font = UIFont.systemFontOfSize(24)
cell.textLabel?.textColor = UIColor.whiteColor()
cell.textLabel?.backgroundColor = UIColor.blackColor()
cell.backgroundColor = UIColor.blackColor()

cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.darkGrayColor().CGColor

return cell

}

最佳答案

您可以将tag分配给您的textField,然后访问它。当 row != 0 时,只需隐藏它即可。这就是我要说的:

if cell.viewWithTag(1001) == nil { // init your text field
FindTextField.frame = ...
FindTextField.tag = 1001 // or whatever you want
...
}
cell.viewWithTag(1001)?.hidden = indexPath.row != 0
...

还有另一种方法 - 在单元格内实现 prepareForReuse() 方法并手动隐藏 FindTextField (它将针对每个单元格隐藏)。稍后在 cellForRow 中,如果 row == 0 则使其可见。

关于ios - 如何在 Swift 2.0 中使用标签将 TextField 添加到 Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com