SWIFT:在UITableView中设置其属性之前,将初始化UITableViewCell

问题描述

我有这个if (WriteProcessMemory(pinfo.hProcess,(LPVOID)(ctx->Ebx + 8),(LPVOID)(&ntHeader->OptionalHeader.ImageBase),4,0))

FARPROC fpNtUnmapViewOfSection = GetProcAddress(GetModuleHandleA("ntdll.dll"),"NtUnmapViewOfSection");

DWORD res = fpNtUnmapViewOfSection(pinfo.hProcess,ImageBase);

如您所见,我正在尝试在每个UITableViewCell显示一个class TableViewCell3: UITableViewCell { var sectionLabel: String? override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) { super.init(style: style,reuseIdentifier: reuseIdentifier) self.setupLabel() } func setupLabel() { let myTextField: UITextField = UITextField(frame: CGRect(x: 0,y: 0,width: 300.00,height: 30.00)); // App crashes here because sectionLabel is nil myTextField.text = sectionLabel! self.contentView.addSubview(myTextField) } }
我要显示属性UITextField设置在UITableViewCell内:

sectionLabel

问题是UITableView在设置extension MoviesViewController5: UITableViewDelegate,UITableViewDataSource { func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell",for: indexPath) as? sectionTableCell2 else { fatalError("Unable to create explore table view cell")} cell.sectionLabel = sectionsLabels![indexPath.row] return cell } } 属性之前被初始化。
因此,当我尝试显示它时:

UITableViewCell

因为它是sectionLabel,所以该应用程序崩溃了。
是的,我确实知道我应该添加 myTextField.text = sectionLabel! 支票,但这不是重点。
要点显示nil属性设置后nil 之后显示方式。

解决方法

最好只是在初始化器中设置UITextfield并在sectionLabel更新时设置其文本。

class TableViewCell3: UITableViewCell {

    private var myTextField: UITextField?

    var sectionLabel: String? {
        didSet {
             self.myTextField?.text = self.sectionLabel
        }
    }
 
    override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
        super.init(style: style,reuseIdentifier: reuseIdentifier)
        self.setupLabel()
    }

    func setupLabel() {
        myTextField = UITextField(frame: CGRect(x: 0,y: 0,width: 300.00,height: 30.00));
        self.contentView.addSubview(myTextField!)
    }
}