-1

I am trying to feed two different tables in a single UIViewController. I am taking an configureCellForTable... exception that is always the case when the cell is nil. But I do not understand which line creates the exception.

My sample code is as below;

static  NSString *cellIdentifier = @"creditCardItemCell";

CreditCardItemTableViewCell *cell;

if(tableView == _creditCardsTableView)
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(tableView == _otherPaymentMethodsTableView)
    cell = [tableView dequeueReusableCellWithIdentifier:@"paymentItemCell"];

Thanks in advance

erdemgc
  • 1,701
  • 3
  • 23
  • 43
  • Have you registered the cell identifier with both the TableViews? – UditS Jan 18 '16 at 13:31
  • @UditS Do I need to register the cellIdentifier? Since I am using Storyboard – erdemgc Jan 18 '16 at 13:32
  • You shouldn't need to register it manually as long as you have specified the "Identifier (Reuse Identifier)" for each cell in the storyboard. – Pandafox Jan 18 '16 at 13:36
  • Have a look at this [answer](http://stackoverflow.com/a/13009288/5060335) – UditS Jan 18 '16 at 13:39
  • @UditS i have tried what they have tried on that answer, but it did not work for me. I also tried moving my cell to .xib file and register, it also did not work. – erdemgc Jan 18 '16 at 15:03
  • If I understand correctly, you have created a xib for your custom cell, specified it's **Reuse Identifier**, registered the xib for both the `_creditCardsTableView` and `_otherPaymentMethodsTableView` using `registerNib: forCellReuseIdentifier:` and used the same **Identifier** as specified in the xib file for both the tableViews. Then you tried to dequeue the cell using same **Identifier** as used in registerNib method. – UditS Jan 18 '16 at 15:13

1 Answers1

0

Multipul tableview are manages in a single UIViewController. but all tableview cell identify must be unique. To manage each tavleview to use following code...

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableview1)
        return [Data1 count];
    else if (tableView == _tableview2)
        return [Data2 count];
    else if (tableView == _tableview3)
        return [Data3 count];

    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify;

    if(tableView == _tableview1)
        identify = @"cell1";
    else if(tableView == _tableview2)
        identify = @"cell2";
    else if(tableView == _tableview3)
    identify = @"cell3";

    table1_TableViewCell *cell = (table1_TableViewCell*)[tableView dequeueReusableCellWithIdentifier:identify];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"table1_TableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

if (tableView == _tableview1)
{
    cell.textLabel.text = @"This is tableview-1";
}
else if (tableView == _tableview2)
{
    cell.textLabel.text = @"This is tableview-2";
}
else if (tableView == _tableview3)
{
    cell.textLabel.text = @"This is tableview-3";
}

return cell;
PT Vyas
  • 722
  • 9
  • 31