Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将UITableView子类化以在其他ViewController中使用_Ios_Swift_Xcode - Fatal编程技术网

Ios 如何将UITableView子类化以在其他ViewController中使用

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创建一个子类,以便在整个应用程序中使用。我的问题是,当我设置
字段
变量时,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调用的?我甚至不会考虑这个。