iOS:如何在故事板中实际没有单元格的情况下使可重用的“默认”单元格出列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22173074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:38:33  来源:igfitidea点击:

iOS: how to dequeue reusable 'default' cells without actually having a cell in storyboard

ioscocoa-touchuikituitableview

提问by OMGPOP

I have several tables the default cells such as the one with title, or the one with icon on the left and title on the right.

我有几个表格的默认单元格,例如带有标题的表格,或者带有左侧图标和右侧标题的表格。

I don't want to add those cells in storyboard and assign identifier to them, is it possible to do that?

我不想在故事板中添加这些单元格并为它们分配标识符,是否可以这样做?

it has to be reusable, I know how to alloc new non-reusable cells

它必须是可重用的,我知道如何分配新的不可重用的单元格

I have tried the answers below,

我已经尝试了下面的答案,

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyCellIdentifier];

should be correct, but it's very tedious and easily forget

应该是正确的,但是很乏味,很容易忘记

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

The above maybe better since it puts all the codes at one place, but when I try dequeueReusableCellWithIdentifier:forIndexPath: (with indexPath) it crashes.

以上可能更好,因为它将所有代码放在一个地方,但是当我尝试 dequeueReusableCellWithIdentifier:forIndexPath: (with indexPath) 时它崩溃了。

  1. why does dequeueReusableCellWithIdentifier:forIndexPath crashes while dequeueReusableCellWithIdentifier does not?
  2. if i don't pass in indexPath, is the cell reusable
  3. if it is reusable, then whats the use of dequeueReusableCellWithIdentifier:forIndexPath?
  1. 为什么 dequeueReusableCellWithIdentifier:forIndexPath 会崩溃而 dequeueReusableCellWithIdentifier 不会?
  2. 如果我不传入 indexPath,单元格是否可重用
  3. 如果它是可重用的,那么 dequeueReusableCellWithIdentifier:forIndexPath 的用途是什么?

回答by Florian Burel

If you don't have any prototype cell in your storyboard, you can use the dequeueReusableCellWithIdentifier: api to create classic cell

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

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

swift:

迅速:

var cell : UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
    cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}

回答by JoeFryer

You can dequeue a UITableViewCell without having a prototype cell in your storyboard. You need to register the cell with your table for a specific identifier (string). You would do this like so:

您可以在故事板中没有原型单元格的情况下使 UITableViewCell 出列。您需要在表格中注册单元格以获取特定标识符(字符串)。你会这样做:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyCellIdentifier];

You could do this in viewDidLoad, maybe.

你可以这样做viewDidLoad,也许。

Then, you can dequeue a cell using that identifier in your cellForRowAtIndexPathmethod as usual.

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

Edit:

编辑:

Where MyCellIdentifieris a constant you have defined somewhere, of course.

MyCellIdentifier当然,您在某处定义的常量在哪里。

回答by Nik Kov

Swift 3.0

斯威夫特 3.0

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
}

It is good, because give cell unwrapped.

这很好,因为让细胞解开。

回答by Aviel Gross

You need to try to dequeue the cell, and if you get nilthan create it. In the cell xib file define it's identifier.

您需要尝试使单元格出列,如果您得到nil了,则创建它。在单元格 xib 文件中定义它的标识符。

Set the cell identifier in the nib file:

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

Set cell identifier

设置单元格标识符

Get a reusable cell or make a new one:

获取一个可重复使用的单元格或制作一个新单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"myCell";
    MyCell *cell = (MyCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSArray *t = [[NSBundle mainBundle] loadNibNamed:@"myCellXibFileName" owner:nil options:nil];
        for (id currentObject in t)
        {
            if ([currentObject isKindOfClass:[MyCell class]])
            {
                cell = (MyCell *)currentObject;
                break;
            }
        }
    }
    // Do whatever you want with the cell....
    return cell;
}

回答by Linc

Open components library, drag a UITableViewCell into your TableView storyboard, and select this cell to open the identity inspector, then select the default style you want and set the cell identifier as same as the dequeueReusableCellWithIdentifier in your code.

打开组件库,将一个 UITableViewCell 拖到你的 TableView storyboard 中,选择这个单元格打开身份检查器,然后选择你想要的默认样式并将单元格标识符设置为与代码中的 dequeueReusableCellWithIdentifier 相同。