S.J
S.J

Reputation: 3071

what are issues involved allocating and initializing subclass of UITableViewCell

- (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

Upvotes: 1

Views: 296

Answers (5)

Oleksii Kuchma
Oleksii Kuchma

Reputation: 98

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.

Upvotes: 1

Greg
Greg

Reputation: 25459

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.

Upvotes: 0

ZeMoon
ZeMoon

Reputation: 20274

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

Upvotes: 0

Vidhyanand
Vidhyanand

Reputation: 993

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

Upvotes: 1

Gajendra Rawat
Gajendra Rawat

Reputation: 3663

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;

    }
}

Upvotes: 1

Related Questions