Create a UIView as a tableview cell with a separate xib file

I need to create a UIView as a tableview cell with a separate xib file. I also tried this with creating a separate xib and designed its correspondence according to the cell type of the tableview. But that failed, is there any programming guide for creating a custom, reusable, creating a table cell?

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

+4
source share
6 answers
  • You need a subclass UITableViewCell, then you have to add a new "View" file (see image below) enter image description here

  • UIView .xib Table View Cell object library.

3.

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 all the XIB should contain).
    cell = [topLevelObjects objectAtIndex:0];
}
+8

NIB . viewDidLoad Nib :

#define kMyCellIdentifier @"kMyCellIdentifier"

....

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

, . Nib, iOS , :

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

  // Configure cell...

  return cell;
}

Storyboards IB, , , iPad, iPhone .

+1

, customcell , .

+1

cellforrowindexpath : -

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

, backgroud

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

. , - .

+1

cellForRowAtIndexPath :

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

First of all, you need to create a file that has the configuration of your class class class class shuold, which will depend on the class UITableViewCell. Create one XIB file that has only a table cell. Then import your cell class socket file into your view or view controller and follow these 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;
}
0
source

All Articles