iOS:如何将可重用的“默认”单元出列,而不在情节提要中实际包含单元

iOS:如何将可重用的“默认”单元出列,而不在情节提要中实际包含单元,ios,cocoa-touch,uikit,uitableview,Ios,Cocoa Touch,Uikit,Uitableview,我有几个默认单元格的表格,比如有标题的表格,或者左边有图标,右边有标题的表格 我不想在情节提要中添加这些单元格并为它们分配标识符,可以这样做吗 它必须是可重复使用的,我知道如何分配新的不可重复使用的细胞 我试过下面的答案 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyCellIdentifier]; 应该是正确的,但它非常乏味,很容易忘记 UITableViewCell *cell

我有几个默认单元格的表格,比如有标题的表格,或者左边有图标,右边有标题的表格

我不想在情节提要中添加这些单元格并为它们分配标识符,可以这样做吗

它必须是可重复使用的,我知道如何分配新的不可重复使用的细胞

我试过下面的答案

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyCellIdentifier];
应该是正确的,但它非常乏味,很容易忘记

UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
   cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
上面的方法可能更好,因为它将所有代码放在一个地方,但是当我尝试使用indexath:forindexath:with indexath退出队列时,它崩溃了

为什么dequeueReusableCellWithIdentifier:forIndexPath崩溃,而dequeueReusableCellWithIdentifier没有崩溃? 如果我不传入indexPath,那么该单元格是否可重用 如果它是可重用的,那么dequeueReusableCellWithIdentifier:forIndexPath有什么用途?
您需要尝试将单元格出列,如果得到nil,则创建它。在cell xib文件中定义其标识符

在nib文件中设置单元格标识符:

获取可重复使用的单元格或创建新单元格:


您可以将UITableViewCell出列,而不必在情节提要中使用原型单元。您需要在表中注册特定标识符字符串的单元格。您可以这样做:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyCellIdentifier];
也许你可以在viewDidLoad中这样做

然后,您可以像往常一样在cellForRowAtIndexPath方法中使用该标识符将单元格出列

编辑:


当然,其中MyCelliIdentifier是您在某处定义的常量。

如果故事板中没有任何原型单元,您可以使用dequeueReusableCellWithIdentifier:api创建经典单元

UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
   cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
斯威夫特:

var cell : UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
    cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}
Swift 3.0
这很好,因为将单元格展开。

打开组件库,将UITableViewCell拖到TableView情节提要中,选择此单元格以打开标识检查器,然后选择所需的默认样式,并将单元格标识符设置为与代码中的dequeueReusableCellWithIdentifier相同。

Swift 4,Swift 5 有4种默认UITableViewCell样式:默认、值1、值2和副标题。了解更多信息,请访问

与Aviels answer类似,但使用-idinitWithStyle:UITableViewCellStyle reuseIdentifier:NSString*reuseIdentifier,而不是从Nib加载。如果使用dequeueReusableCellWithIdentifier:方法,则无需注册类。但是,如果使用dequeueReusableCellWithIdentifier:forIndexPath,则确实需要注册:这两个方法都将为您提供一个可重用单元,但前一个方法将为您实例化它,因此您需要注册一个类或原型单元。第一个方法将获取重用堆栈中具有给定标识符的单元格,并返回nil如果不存在,则必须自己实例化该单元格,因此只能在第一次分配该单元格时,然后,所有其余的都将退出队列?您确定不需要注册类吗?如果使用dequeueReusableCellWithIdentifier:方法,则不需要注册类。但是,如果使用dequeueReusableCellWithIdentifier:forIndexPath,则确实需要注册:这两个方法都将为您提供一个可重用单元,但前一个方法将为您实例化它,因此您需要注册一个类或原型单元。第一种方法将获取重用堆栈中具有给定标识符的单元,并返回nil如果不存在,则必须自己实例化该单元。这个问题的全部思想是在xib或故事板中不包含单元。您的问题没有提到xib,只有故事板,由于storyBoard通过只调用dequeueReusableCellWithIdentifier来管理不同于xib的单元,并始终获取一个单元,我从您的问题中了解到,您寻求为单元使用xib文件,而不是将其放在storyBoard的tableView中。下次请说得更清楚些。我不会称为注册UITableViewCell来获取标识符非常繁琐,这只是一行代码:这段代码完全跳过了单元格池中的重用。
var cell : UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
    cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
        return UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        return cell
    }()
    
    cell.textLabel?.text = anyArray[indexPath.row]
    
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")
    cell.textLabel?.text = user[indexPath.row].name
    cell.detailTextLabel?.text = ">"
    return cell
}