Question

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 code at one place, but when I try dequeueReusableCellWithIdentifier:forIndexPath: (with indexPath) it crashes.

  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?
Was it helpful?

Solution

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

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)
}

OTHER TIPS

Swift 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.

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:

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

You could do this in viewDidLoad, maybe.

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

Edit:

Where MyCellIdentifier is a constant you have defined somewhere, of course.

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

Set the cell identifier in the nib file:

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;
}

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.

Swift 4, Swift 5

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
}

There are 4 Default UITableViewCell Style: default, value1, value2 & subtitle. Learn more at https://developer.apple.com/documentation/uikit/uitableviewcell/cellstyle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top