1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    cell.firstLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    cell.secondLabel.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS - indexPath.row];

    return cell;
}

this is code snippet from Apple Table View Programming Guide

MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; is working fine and need no checking against nil because the cell is defined in story board and alway return valid cell.

But if I am not using story board, programmatically how I will use multiple custom cells in my tableview? And what are issues involved allocating and initializing MyTableViewCell

S.J
  • 3,063
  • 3
  • 33
  • 66

5 Answers5

1

You can use your own custom cell in this way also

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    MyTableViewCell *cell=(MyTableViewCell *)[self.yourtableview  dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:0];//change as per your need

   if(cell==nil)
    {
    [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
    cell=self.mytableviewcellref;
    }
    cell.textLabel.text=@"sometext";
    return cell;
}

Hope it helps you..

Vidhyanand
  • 993
  • 10
  • 21
  • @ SJ. you need to load Nib while using custom cell. – Vidhyanand Mar 26 '14 at 11:52
  • Please tell me why you did not alloc init subclass of uitableviewcell and how this loadNibNamed works? – S.J Mar 26 '14 at 11:55
  • you are connecting the custom cell directly at its cell.xib to your view controller. So you can just load its Nib here. If you are using default UITableViewCell then you must alloc to use it if it is nil..@SJ.. – Vidhyanand Mar 26 '14 at 12:05
  • I have subclass of uitableviewcell and it has nib also, I want to create 5 different kind of cells, should I create 5 different subclass of uitableviewcell? – S.J Mar 26 '14 at 12:11
  • You need to create 5 different xibs to your custom cell and load them using [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil]; ///change MYTableViewCell as per xib name. for each indexpath – Vidhyanand Mar 26 '14 at 12:44
1

If you want create cell programatically then you need to allocate and initialised table cell like this.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *identifier = @"cell";

    UITableViewCell *cell = [listtableview dequeueReusableHeaderFooterViewWithIdentifier:identifier];

    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
}
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
  • I am using custom cell MyTableViewCell, not just a regular cell. – S.J Mar 26 '14 at 11:47
  • ok no problem :) in this case you have to make a saprate class of Custom cell please check this link here you can find how can we add custom cell http://mrmaksimize.com/ios/Custom-UITableViewCell-With-NIB/ – Gajendra Rawat Mar 26 '14 at 11:50
  • I want to do all programmatically, no NIB or storyboard. – S.J Mar 26 '14 at 11:52
  • ok then in case you should add your cell element(label, image) programatically in custom cell class. check this for more detail http://stackoverflow.com/questions/18969355/how-to-create-a-custom-uitableviewcell-programmatically-using-autolayout – Gajendra Rawat Mar 26 '14 at 11:55
  • check also http://jslim.net/blog/2013/03/22/ios-create-uitableview-with-custom-cell-programmatically/ :D – Gajendra Rawat Mar 26 '14 at 11:56
1

You should use methods

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

of UITableView. You can read documentation here.

When you called method

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

, it checks if there is available cell in reuse queue. If not, it checks if it can create this cell automatically. If you have register cell class or nib for this reuse identifier before, it will create new cell using class or nib and return it. If you haven't register anything, it will return nil.

It is better to use registering, because, if you have different custom cells for different reuse identifiers, code for creating these cells becomes messy. Also it's the right way. Methods for registering were added in iOS5 and iOS6 respectively. Code for creating custom cell by programmer is related to old versions of iOS.

  • please can you tell in bit detail how these method works and why not alloc init subclass of uitableviewcell, and how you find these methods :p – S.J Mar 26 '14 at 11:59
0

There is another method which you can use:

MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"> forIndexPath:indexPath];

You pass additional parameter - indexPath. After that check if cell is nil and if so you allocate and initialise it.

Greg
  • 25,317
  • 6
  • 53
  • 62
0

If you are not using a storyboard, then you need to check the cell against nil and if so, allocate a new cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"MyIdentifier";
    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.firstLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    cell.secondLabel.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS - indexPath.row];

    return cell;
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98