Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.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 Swift无法将单元格出列_Ios_Swift_Uitableview - Fatal编程技术网

Ios Swift无法将单元格出列

Ios Swift无法将单元格出列,ios,swift,uitableview,Ios,Swift,Uitableview,我为表格视图创建了一个扩展,用于轻松注册单元格: extension UITableView { func mapTableViewWithCellsDictionary(dictionary : [String : String]) -> Void { for (key, _) in dictionary { self.register(NSClassFromString(dictionary[key]!), forCellReuseI

我为表格视图创建了一个扩展,用于轻松注册单元格:

extension UITableView {

    func mapTableViewWithCellsDictionary(dictionary : [String : String]) -> Void {

        for (key, _) in dictionary {
            self.register(NSClassFromString(dictionary[key]!), forCellReuseIdentifier: key)
        }

    }
}
在我的视图控制器中,我执行了以下操作:

  let dct = ["VacanciesItem":"VacanciesCell"]
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        tableView.separatorStyle = .none
        tableView.mapTableViewWithCellsDictionary(dictionary: dct)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 80
        tableView.rowHeight = UITableViewAutomaticDimension
        self.view.addSubview(tableView)
然后:

但是,应用程序无法看到该单元格,它会抛出一个错误:

'unable to dequeue a cell with identifier VacanciesItem - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
但是我已经注册了。

错误在这里

for (key, _) in dictionary {
        self.register(NSClassFromString(dictionary[key]!), forCellReuseIdentifier: key)
    }
您正在使用
vacanceItem
作为类和重用标识符。我想你是说

for (key, value) in dictionary {
        self.register(NSClassFromString(dictionary[value]!), forCellReuseIdentifier: key)
    }

因此,您将使用
vacancestem
标识符注册
vacancescell
类首先,在下面的代码行中,
键和值对指定了什么

  let dct = ["VacanciesItem":"VacanciesCell"]
要注册
xib
,如果要使用
xib
创建单元格,则需要指定
UINib
对象

考虑到在
dct
中,
键(“真空项”)
xib名称
值(“真空项”)
单元标识符

   func mapTableViewWithCellsDictionary(dictionary : [String : String]) -> Void
   {
            for (nibName, identifier) in dictionary
            {
                self.register(UINib(nibName: nibName), forCellReuseIdentifier: identifier)
            }
   }
如果使用代码创建UITableViewCell,则要注册它,请使用:

    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String;
    self.tableView.register(NSClassFromString("\(namespace).\(nibName)") as? UITableViewCell.Type, forCellReuseIdentifier: identifier)

在swift中,您还需要添加
名称空间(项目名称)
文件名
,以对其进行标识。

请按以下方式更改扩展名

extension UITableView {

    func mapTableViewWithCellsDictionary(dictionary : [String : String]) -> Void {

        for (key, _) in dictionary {
            self.register(UINib(nibName: String(describing:dictionary[key]!), bundle: nil), forCellReuseIdentifier: key)
        }

    }
}

您是否在属性检查器中指定了单元标识符?您正在注册不同的单元类名,并且正在取消某些其他类名,我怀疑我在这里不使用故事板或xib,而是以编程方式创建单元。@PGDev“要注册xib,您需要指定UINib对象,而不是类名。”-@mag_zbc我以为他在用xib制造细胞。谢谢你。我会纠正的。什么问题?我假设
vacancestem
是重用标识符,
vacancescell
是单元类。我弄错了吗?哪个是哪个?
extension UITableView {

    func mapTableViewWithCellsDictionary(dictionary : [String : String]) -> Void {

        for (key, _) in dictionary {
            self.register(UINib(nibName: String(describing:dictionary[key]!), bundle: nil), forCellReuseIdentifier: key)
        }

    }
}