Frage

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?

War es hilfreich?

Lösung

  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];
}

Andere Tipps

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.

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.

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.

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

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top