0

I have XIB that contain UITableView and in same XIB i created UITableViewCell, I did not set any class to cell, I just set identifier to the cell.

Now I want to load that cell to tableview delegate methd cellForRowAtIndexPath

i did is :

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    return cell;
}

but it create new cell that doesn't include xib that i created.

So how do i get that cell from same xib that table view has ?

Dx Android
  • 61
  • 2
  • 13

3 Answers3

0

If you are creating a custom cell, then it will be better if you create custom classes of UITableViewCell type and then instantiate your UITableViewCell class in method.If you dont want to create custom class then below is the code that can help you:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CustomCellIdentifier = @"SimpleTableItem";
   UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
   if (cell == nil)
   {
     cell = (UITableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"TMPartnerLoyalty" owner:self options:nil] objectAtIndex:0];
   }
   return cell;

}  

Here, TMPartnerLoyalty is name of the xib.

Milan Gupta
  • 1,181
  • 8
  • 21
  • i did'nt set any class to cell, i want to use same class that table view have. – Dx Android Mar 26 '16 at 04:35
  • I have edited the answer, to use UITableViewCell class and do not create custom class. – Milan Gupta Mar 26 '16 at 04:40
  • it just calling cellForRowAtIndexPath infinite time..!!! not stopped. i added static 10 to numberOfRowsInSection. i think it's because of loading nib, that load tableview every time. – Dx Android Mar 26 '16 at 04:46
0

please write this code in viewdidload , i hope it will work for you.

NSString *nibName = @"give your xib name here that contain Tablecell";
NSString *simpleTableIdentifier = @"SimpleTableItem";
UINib *customCellNib = [UINib nibWithNibName:nibName bundle:nil];
[tableView registerNib:customCellNib forCellReuseIdentifier:simpleTableIdentifier];
Satyanarayana
  • 1,059
  • 6
  • 16
0

If you use prototype cell,Identifier name must same as dequeueReusableCellWithIdentifier name

NOTE:If you use prototype cell, you don't need to register the cell.Because Prototype cell is not a CustomCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   if(cell == nil)
   {
     cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
   cell.textLabel.text = @"";
   return cell;
}

If you use UITableViewcell(Custom cell),follow the below steps

1.in viewDidLoad you must register the UITableViewCell or Custom Cell

UINib *cellNib = [UINib nibWithNibName:@"YourUITableViewCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"cell"];

2.OR in cellForRowAtIndexPath you can write the code below like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *customTableIdentifier=@"cell";
  YourUITableViewCell *cell=(YourUITableViewCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
  if (cell==nil)
  {
    NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"YourUITableViewCell" owner:self options:nil];
    cell=[nibs objectAtIndex:0];
  } 
  cell.yourTableViewCellLabel.text = @"";
  cell.yourTableViewCellTextField.text = @"";
  cell.yourTableViewCellButton.text = @"";
  return cell;
}
user3182143
  • 9,459
  • 3
  • 32
  • 39