Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register nib name for tableView

static NSString *cellIdentifier = @"cell";
if (tableView ==tableview1) 
{
    ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];        
    if (cell1 == nil) 
    {
        [[NSBundle mainBundle] loadNibNamed:@"ContactCustom" owner:self options:nil];
        cell1 = contactCustom;
    }
}

How to register nib name in viewDidLoad method before calling cellForRowAtIndex method?

like image 395
user2134883 Avatar asked Mar 12 '13 12:03

user2134883


People also ask

How do I register Tableview?

There are two variants to register , but both take a parameter called forCellReuseIdentifier , which is a string that lets you register different kinds of table view cells. For example, you might have a reuse identifier "DefaultCell", another one called "Heading cell", another one "CellWithTextField", and so on.

How do I register a custom cell in Swift?

Registering the cell We need a way to tell the UITableView to use our new cell instead of the default cell. To do that we register the XIB file in the view controller that has the UITableView . Then, open HabitsTableViewController. swift to finish registering the cell.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


3 Answers

-(void)viewDidLoad
{
   [super viewDidLoad];
   [self.tableView registerNib:[UINib nibWithNibName:@"cell" bundle:nil] 
   forCellReuseIdentifier:@"cell"];
}
like image 165
Gabriel Avatar answered Oct 07 '22 13:10

Gabriel


Apple provided register nib method for UITableView after IOS 5 Please check class reference http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

Ex:

     In view did load you can register nib for UITableView like below
     [tableView registerNib:[UINib nibWithNibName:@"nibName" bundle:nil] forCellReuseIdentifier:@"identifienName"];

      In cellForRowAtIndexPath
      cell =  [tableView dequeueReusableCellWithIdentifier:@"identifienName"];
like image 29
Narayana Avatar answered Oct 07 '22 13:10

Narayana


For Swift

tableViewSubCategory.register(UINib(nibName: "", bundle: nil), forCellReuseIdentifier: "")
like image 31
Ucdemir Avatar answered Oct 07 '22 13:10

Ucdemir