user3388273
user3388273

Reputation: 399

uitable view inside custom cell

I am using a table view inside custom cell.The below code specifies the table inside custom cell.

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

    static NSString *CellIdentifier = @"commnetCell";
    commnetCell *cell = (commnetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure Cell
    [cell.lbl_comment setText:@"kool"];

    return cell;
}

here cell returns nill.I have set the delegate and datasource.

Upvotes: 1

Views: 68

Answers (1)

Rafał Sroka
Rafał Sroka

Reputation: 40030

You have to allocate the cell if there is none in the reuse queue.

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

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }

    // Configure the cell.

    return cell;
}

Alternatively, you can create a prototype in the storyboard or register a custom cell class. Then, you don't have to allocate it on your own. It will be created for you when you request it using the dequeueReusableCellWithIdentifier method.

The dequeueReusableCellWithIdentifier: will always return a cell if you have called registerClass: with the corresponding identifier.

Upvotes: 1

Related Questions