关于带有XIBSwift的UITableViewCell子类和swiftui列表的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于BFPaperTableViewCellUITableVie
关于带有XIB Swift的UITableViewCell子类和swiftui 列表的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于BFPaperTableViewCell UITableViewCell 子类、iOS Swift – UITableViewCell自定义子类不显示内容、ios – Swift /如何使用动态高度的UITextView INSIDE UITableViewCell具有动态高度、ios – Swift TableViewCell xib没有约束等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- 带有XIB Swift的UITableViewCell子类(swiftui 列表)
- BFPaperTableViewCell UITableViewCell 子类
- iOS Swift – UITableViewCell自定义子类不显示内容
- ios – Swift /如何使用动态高度的UITextView INSIDE UITableViewCell具有动态高度
- ios – Swift TableViewCell xib没有约束
带有XIB Swift的UITableViewCell子类(swiftui 列表)
我有一个使用自定义方法连接到xib 的UITableViewCell
子类。NameInput``init
class NameInput: UITableViewCell { class func make(label: String, placeholder: String) -> NameInput { let input = NSBundle.mainBundle().loadNibNamed("NameInput", owner: nil, options: nil)[0] as NameInput input.label.text = label input.valueField.placeholder = placeholder input.valueField.autocapitalizationType = .Words return input }}
有没有办法可以在viewDidLoad
方法中初始化此单元格并仍然重用它?还是我必须使用重用标识符注册类本身?
答案1
小编典典惯用的NIB流程为:
- 使用重用标识符注册您的NIB。在Swift 3中
override func viewDidLoad() {super.viewDidLoad()tableView.register(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell")
}
在Swift 2中:
override func viewDidLoad() { super.viewDidLoad() tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell")}
定义您的自定义单元格类:
import UIKit
class NameInput: UITableViewCell {
@IBOutlet weak var firstNameLabel: UILabel!@IBOutlet weak var lastNameLabel: UILabel!
}
在Interface Builder中创建一个NIB文件(具有在步骤1中引用的相同名称):
在NIB中指定表视图单元的基类以引用您的自定义单元类(在步骤2中定义)。
将NIB单元中控件之间的
@IBOutlet
引用与自定义单元类中的引用连接起来。
cellForRowAtIndexPath
然后,您将实例化单元格并设置标签。在Swift 3中override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NameInputlet person = people[indexPath.row]cell.firstNameLabel.text = person.firstNamecell.lastNameLabel.text = person.lastNamereturn cell
}
在Swift 2中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NameInput let person = people[indexPath.row] cell.firstNameLabel.text = person.firstName cell.lastNameLabel.text = person.lastName return cell}
从您的示例中不能完全确定您在单元格上放置了哪些控件,但是上面有两个UILabel
控件。连接@IBOutlet
对您的应用有意义的所有引用。
BFPaperTableViewCell UITableViewCell 子类
BFPaperTableViewCell 介绍
BFPaperTableViewCell 是 UITableViewCell 的子类,灵感来源于 Google 的 Paper Material
Design。
BFPaperTableViewCell 官网
https://github.com/bfeher/BFPaperTableViewCell
iOS Swift – UITableViewCell自定义子类不显示内容
屏幕截图将向您显示我将第一个UITableViewCell的样式设置为Subtitle,第二个设置为自定义,并在中心添加标签“Tap to Add”.第一个具有标识符“Cell”和第二个“AddCell”.我已经设置了一个UITableViewController(我也在UIViewController中尝试了UITableView),Swift中的UITableViewCell子类,我连接了所有的插座.但是,当我运行模拟器时,单元格已加载并且它是可点击的,但我无法让它显示任何内容. (我已尝试添加其他控件,但在加载单元格时不会出现任何内容.我唯一能改变的是contentView的backgroundColor.)
我有UITableViewController的以下Swift代码:
import UIKit class ListTableViewController: UITableViewController { var listObjects: ListObject[] = DataManager.instance.allListObjects() as ListObject[] init(style: UITableViewStyle) { super.init(style: style) // Custom initialization } init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder) } override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(AddListObjectTableViewCell.classForCoder(),forCellReuseIdentifier: "AddCell") } @IBAction func editButtonpressed(editButton: UIBarButtonItem) { if (self.tableView.editing) { editButton.title = "Edit" self.tableView.setEditing(false,animated: true) } else { editButton.title = "Done" self.tableView.setEditing(true,animated: true) } } // #pragma mark - Table view data source override func numberOfSectionsInTableView(tableView: UITableView?) -> Int { return 1 } override func tableView(tableView: UITableView?,numberOfRowsInSection section: Int) -> Int { return listObjects.count + 1 } override func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let cellIdentifier = (indexPath.row < listObjects.count) ? "Cell" : "AddCell" var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath: indexPath) as UITableViewCell if (indexPath.row < listObjects.count) { let currentListObject : ListObject = listObjects[indexPath.row] cell.textLabel.text = currentListObject.name cell.detailTextLabel.text = currentListObject.detail } else { cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath: indexPath) as AddListObjectTableViewCell if (cell == nil) { cell = AddListObjectTableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: cellIdentifier) } } return cell } override func tableView(tableView: UITableView!,didSelectRowAtIndexPath indexPath: NSIndexPath!) { if (indexPath.row < listObjects.count) { } else { self.performSegueWithIdentifier("AddListObjectShow",sender: self) } tableView.deselectRowAtIndexPath(indexPath,animated: true) } // Override to support conditional editing of the table view. override func tableView(tableView: UITableView?,canEditRowAtIndexPath indexPath: NSIndexPath?) -> Bool { return (indexPath?.row < listObjects.count) ? true : false }}
我的UITableViewCell也有以下Swift:
import UIKit class AddListObjectTableViewCell: UITableViewCell { @IBOutlet var addLabel : UILabel init(style: UITableViewCellStyle,reuseIdentifier: String) { super.init(style: style,reuseIdentifier: reuseIdentifier) // Initialization code } override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool,animated: Bool) { super.setSelected(selected,animated: animated) // Configure the view for the selected state }}
最后,这是模拟器的屏幕截图,选中时可见空单元格:
我已经仔细检查了所有我的所有插座都已连接,我的类名在Interface Builder中正确设置,我已经使用tableView注册了我的UITableViewCell类,所有内容似乎都配置正确.这可能是一个错误吗?任何帮助将不胜感激.
解决方法
在故事板中,尝试将宽度/高度设置为任何/任何或在检查器中,单元格内的标签一直向下滚动并播放“已安装”复选框,您会发现它有大小类的各种选项.如果它像我的一样,你将第一个’已安装’一个未经检查,另一个为你的尺寸选项选中.我删除了自定义’已安装’并检查了默认设置,然后将标签移动到位.
我不相信我有足够的声誉来发布超过1个图像或2个链接,希望我能解释我的意思更容易.
ios – Swift /如何使用动态高度的UITextView INSIDE UITableViewCell具有动态高度
tableView.estimatedRowHeight = 155 tableView.rowHeight = UITableViewAutomaticDimension
这段代码让UITextView根据内容改变它的高度.
extension NewUserTweetTableVC: UITextViewDelegate { func textViewDidChange(textView: UITextView) { let fixedWidth = textView.frame.size.width textView.sizeThatFits(CGSize(width: fixedWidth,height: CGFloat.max)) let newSize = textView.sizeThatFits(CGSize(width: fixedWidth,height: CGFloat.max)) var newFrame = textView.frame newFrame.size = CGSize(width: max(newSize.width,fixedWidth),height: newSize.height) textView.frame = newFrame } }
一旦我在运行时更改了UITextView的文本,UITextView的高度就会改变,但UITableViewCell的高度却没有.我怎么能改变呢?非常感谢帮助.
解决方法
let textView = UITextView()//your Text view let sizeThatShouldFitTheContent = textView.sizeThatFits(textView.frame.size) let height = sizeThatShouldFitTheContent.height
试试这个演示……
在UITableView中自定义UITextView
http://candycode.io/self-sizing-uitextview-in-a-uitableview-using-auto-layout-like-reminders-app/
ios – Swift TableViewCell xib没有约束
TableViewController
let cellIdentifier = "cellID" override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: cellIdentifier,bundle: nil),forCellReuseIdentifier: cellIdentifier) tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 300 }
故事板中的原型单元(用于工作)
复制粘贴到XIB时的单元格
约束
XIB视图层次结构
解决方法
在您的问题标题中,您询问了如何解决“Swift TableViewCell xib没有约束”的问题.此外,在赏金中,您指定答案应该是:
An adequate answer showing why constraints are lost when moved to a
xib,when they exist in the xib,and how to address this problem.
根据我的理解,你的问题有两个部分:
>自定义TableViewCell xib没有约束的问题
>从Storyboard原型复制/粘贴到xib时,为什么会丢失约束
对于问题#1,我使用了几乎所有与您在自定义单元格xib中的屏幕截图中显示的相同的约束,并且它有效.我将在下面详细解释.
对于问题#2,我发现约束不会丢失.您可能需要共享您的项目,以便其他人可以复制您遇到的问题.我将具有类似约束的Storyboard原型单元复制/粘贴到xib上,并且我没有任何约束问题.所以我可以确认复制/粘贴功能是否有效.
解决方案演示
我创建了以下演示来测试您的问题.请参阅下面的屏幕截图. tableView包含六个单元格.第一个和第二个是相同的,并具有重用标识符MyCell.第3和第4个是相同的,并具有重用标识符MycopyPasteCell.第5和第6个是相同的并且具有重用标识符MyPrototypeCell.
MyCell存在于xib中,并且几乎使用了屏幕截图中显示的所有相同约束.如您所见,没有约束问题. MycopyPasteCell使用类似的约束,并从Storyboard原型复制/粘贴到xib. MyPrototypeCell是复制的原始原型.即使将原型复制到xib,最后四个单元看起来完全相同.从原型到xib的约束没有问题.
约束
在下面的代码中,我列出了您在ContentView的屏幕截图中显示的所有约束. (请注意,我还实现了height = 20,aspect = 1:1和height = 75约束,尽管它们未在下面列出.)由于我没有使用它们,因此注释掉了两个约束.我还添加了一个约束来替换另一个未使用的约束.
// bottomMargin = Profile Image View.bottom + 188.5 bottomMargin = Profile Image.bottom + 17 Profile Image View.top = Username Label.top Profile Image View.leading = leadingMargin + 2 Profile Image View.top = topMargin + 20 Username Label.leading = Content Text View.leading // Username Label.top = topMargin + 20 Username Label.trailing = Content Text View.trailing Username Label.leading = Profile Image View.trailing + 15 trailingMargin = Username Label.trailing + 10 Content Text View.top = Username Label.bottom + 5 Content Text View.leading = leadingMargin + 92 bottomMargin = Content Text View.bottom + 20
第一个注释约束// bottomMargin = Profile Image View.bottom 188.5没有意义,因为它会将图像的底部与单元格的底部分开188.5.这也与Storyboard(用于工作)截图中的Prototype单元格不匹配.我将它替换为bottomMargin = Profile Image.bottom 17,它将188.5替换为17.
第二个注释约束// Username Label.top = topMargin 20(将username和topMargin分隔为20)在技术上可以工作.但是,它的功能对于Profile Image.top = topMargin 20(将Profile Image和topMargin分隔为20)和Profile Image View.top =用户名Label.top(将Profile Image和Username设置为相同的顶部分隔,即通过20).
MyTableViewController
以下是我的视图控制器.基本上我创建了三个部分,每个部分有两个单元格/行. MyCell和MycopyPasteTableViewCell来自xib,并在viewDidLoad()中注册. MyPrototypeCell来自Storyboard,未在viewDidLoad()中注册.
// MyTableViewController.swift import UIKit class MyTableViewController: UITableViewController { let myCell = "MyCell" let mycopyPasteCell = "MycopyPasteTableViewCell" let myPrototypeCell = "MyPrototypeCell" override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: myCell,forCellReuseIdentifier: myCell) tableView.register(UINib(nibName: mycopyPasteCell,forCellReuseIdentifier: mycopyPasteCell) tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 300 } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 3 } override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { return 2 } override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: myCell,for: indexPath) return cell } else if indexPath.section == 1 { let cell = tableView.dequeueReusableCell(withIdentifier: mycopyPasteCell,for: indexPath) return cell } else { let cell = tableView.dequeueReusableCell(withIdentifier: myPrototypeCell,for: indexPath) return cell } } }
Github链接
https://github.com/starkindustries/TableViewCellConstraintsTest
今天关于带有XIB Swift的UITableViewCell子类和swiftui 列表的分享就到这里,希望大家有所收获,若想了解更多关于BFPaperTableViewCell UITableViewCell 子类、iOS Swift – UITableViewCell自定义子类不显示内容、ios – Swift /如何使用动态高度的UITextView INSIDE UITableViewCell具有动态高度、ios – Swift TableViewCell xib没有约束等相关知识,可以在本站进行查询。
本文标签: