4

I need to design a UIView as the tableview cell with separate xib file. I also tried it with creating a separate xib and design its view matches to tableview cell type. But it is failed, there is any programming guide to create a custom, reusable, tableview cell creation?

After creating custom tableview cell how can we add it to the table view?

Arunkumar
  • 55
  • 1
  • 8
  • This should be helpful: [ios steps to create custom UITableViewCell with xib file](http://stackoverflow.com/questions/4917027/ios-steps-to-create-custom-uitableviewcell-with-xib-file) – Amar Oct 09 '13 at 07:31

6 Answers6

8
  1. You need to subclass UITableViewCell then you've to add a new 'View' file (refer to image below) enter image description here

  2. Delete the UIView file in .xib and add Table View Cell from object library.

3.Set the custom class for your cell

enter image description here

CustomCell *cell = (CustomCell*)[tableView    dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    // Load the top-level objects from the custom cell XIB.
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
    cell = [topLevelObjects objectAtIndex:0];
}
Ankur Arya
  • 4,693
  • 5
  • 29
  • 50
  • @TheDoctor I follow creating customCell with xib @ http://www.weheartswift.com/swifting-around. In this tutorial he don't implement `func ... heightForRowAtIndexPath`. But my cells are overlapping if I don't use this function. what's wrong with my code ? – Emadpres Jul 11 '14 at 18:42
  • @EmAdpres: The default height of a cell is 44, if your custom cell is of different height you need to either implement `heightForRowAtIndexPath` or set cell height for the tableview in interface builder. – Ankur Arya Jul 13 '14 at 03:17
  • @TheDoctor I set cell height for the tableview in interface builder and still it overlapping. I think I have no other choice than implementing `heightForRowAtIndexPath` as my list is not static [according to this answer](http://stackoverflow.com/questions/8615862/custom-cell-row-height-setting-in-storyboard-is-not-responding). Am I right or I have still chance to set cell height in interface builder ? – Emadpres Jul 13 '14 at 07:03
  • heightForRowAtIndexPath is used for dynamically changing the height of cell at runtime. If you want the cells in the tableView to be of different height you must implement this method. – Ankur Arya Jul 13 '14 at 07:13
1

You can create a separate NIB to store your table cell. In viewDidLoad register your Nib file for each cell type:

#define kMyCellIdentifier @"kMyCellIdentifier"

....

- (void)viewDidLoad
{
  UINib *tableCellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
  [self.tableView tableCellNib forCellReuseIdentifier:kMyCellIdentifier];
}

Then, you can create that cell when asked for. Since you've registered the Nib iOS will handle creating the cell for you if there isn't already one to be dequeued:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMyCellIdentifier];

  // Configure cell...

  return cell;
}

You can also create prototype cells in Storyboards directly in IB, but ff you have a universal application it's a pain since you'll need to maintain both an iPad and an iPhone version of the same cell.

tarmes
  • 15,366
  • 10
  • 53
  • 87
1

you can find a demo here and there is a customcell folder in side shared and hope fully you'll be able to find all the stuff and fix it as per your need.

D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
1

in cellforrowindexpath method of delegate method add as below:-

UIView *myView = [UIView alloc]init]; //or initwithframe
myview.frame = cell.contentView.frame;

if want to make sure then set backgroud color

myview.backgroundColor = [UIColor yellowColor];  //Or any other color.
[cell addsubview :myview];

Thank you. Feel free to contact if you have any query.

Shreyansh Shah
  • 101
  • 1
  • 11
0

In the cellForRowAtIndexPath delegate method, do the following:

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

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


    UIView *view =[[UIView alloc]init];
    view.frame = cell.contentView.frame;

    view.backgroundColor = [UIColor redColor];//To be sure that the custom view in the cell
    [cell addSubview:view];

    return cell;
}
Mutawe
  • 6,464
  • 3
  • 47
  • 90
0

Very first you have to create a file having configuration of you cell class class shuold be inharited from UITableViewCell class.Create One XIB file having only table view cell.Then import your custom cell class file into your view or view controller and implement following methods.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
            YOUR_CUSTOM_CELL_CLASS * cell = (YOUR_CUSTOM_CELL_CLASS *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            return [self createCustomCell:cell cellForRowAtIndexPath:indexPath];
}


-(YOUR_CUSTOM_CELL_CLASS *)createCustomCell:(YOUR_CUSTOM_CELL_CLASS *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
if (cell == nil)
    {
        cell = (YOUR_CUSTOM_CELL_CLASS *)[[[NSBundle mainBundle] loadNibNamed:@"YOUR_CUSTOM_CELL_CLASS_XIB_FILE_NAME" owner:self options:nil] objectAtIndex:0];
    }
    return cell;
}
Jekil Patel
  • 403
  • 4
  • 18