Ios 如何将UITableView子类化以在其他ViewController中使用
我希望为tableview创建一个子类,以便在整个应用程序中使用。我的问题是,当我设置Ios 如何将UITableView子类化以在其他ViewController中使用,ios,swift,xcode,Ios,Swift,Xcode,我希望为tableview创建一个子类,以便在整个应用程序中使用。我的问题是,当我设置字段变量时,tableview仍然为空。我假设字段变量在新的ViewController中设置时设置不正确。提前谢谢你的帮助 MySubClass import UIKit import LGButton class RquestTableViewController: UITableViewController { var fields:[UIView] override func vie
字段
变量时,tableview仍然为空。我假设字段
变量在新的ViewController中设置时设置不正确。提前谢谢你的帮助
MySubClass
import UIKit
import LGButton
class RquestTableViewController: UITableViewController {
var fields:[UIView]
override func viewDidLoad() {
super.viewDidLoad()
viewSetup()
}
func viewSetup(){
tableView.register(CustomFormCell.self, forCellReuseIdentifier: labelReuseCellIdentifier)
self.tableView.tableFooterView = UIView.init(frame: CGRect.zero)
}
}
//MARK: Delegate
extension RquestTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return fields.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: self.labelReuseCellIdentifier, for: indexPath) as! CustomFormCell
let itemView = fields[indexPath.row]
cell.viewPassed = itemView
cell.backgroundColor = .clear
if let _ = itemView as? UILabel {
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
return cell
}
if let _ = itemView as? UIButton {
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
return cell
}
if let _ = itemView as? LGButton {
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
return cell
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40.0
}
}
MyNewViewController
class NewRquestViewController: RquestTableViewController {
lazy var password:JVFloatLabeledTextField = {
let v = JVFloatLabeledTextField()
v.placeholder = "Password"
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
fields = [password]
}
}
您是否已验证
字段
的设置是否确实不正确?我看没有理由不这样。但我确实看到了另一个问题:除了覆盖numberOfSections(in:)
和tableView(u,cellForRowAt:)
,您还必须覆盖tableView(u,numberofrowsinssection:)
。您现在拥有的是返回字段数量的numberOfSections(in:)
,但是tableView(u,cellForRowAt:)
希望用字段填充行。这意味着只有当section
为0
时才会调用tableView(u,cellForRowAt:)
,因此最多只能看到一个字段。相反,将numberOfSections(in:)
更改为返回1
,并实现tableView(\uu,numberofrowsinssection:)
以返回字段。count
是否验证了字段的设置是否正确?我看没有理由不这样。但我确实看到了另一个问题:除了覆盖numberOfSections(in:)
和tableView(u,cellForRowAt:)
,您还必须覆盖tableView(u,numberofrowsinssection:)
。您现在拥有的是返回字段数量的numberOfSections(in:)
,但是tableView(u,cellForRowAt:)
希望用字段填充行。这意味着只有当section
为0
时才会调用tableView(u,cellForRowAt:)
,因此最多只能看到一个字段。相反,将numberOfSections(in:)
更改为返回1
,并实现tableView(\uu,numberofrowsinssection:)
以返回字段。count
您需要重新加载数据您需要重新加载数据工作正常谢谢,请注意必须从新的viewControllerIt调用registerCell?我甚至不会考虑这个问题。让它工作起来了,谢谢,只是要注意,registerCell必须是从新的viewControllerIt调用的?我甚至不会考虑这个。