Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

  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?
like image 916
OMGPOP Avatar asked Mar 04 '14 13:03

OMGPOP


3 Answers

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) } 
like image 100
Florian Burel Avatar answered Sep 20 '22 20:09

Florian Burel


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.

like image 37
Nik Kov Avatar answered Sep 22 '22 20:09

Nik Kov


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.

like image 26
JoeFryer Avatar answered Sep 19 '22 20:09

JoeFryer