Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UiTableViewCell from nib without reuse?

I need to load the custom cell with a NIB associated in a table without using "dequeueReusableCellWithIdentifier:"

The cells must be loaded new, do not want to reuse the old ..

suppose that the class is called "CustomCell.h and CustomCell.m" and the NIB CustomCell.xib

How can I allocate and initialize the cell does not use them?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

CustomCell *cell = ??

//cell customization

return cell

}

How to solve the issue?

like image 377
user3228179 Avatar asked Feb 13 '23 10:02

user3228179


1 Answers

You can load CustomCell with loadNibNamed method of NSBundle as follow.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

CustomCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"NibFile" owner:self options:nil] objectAtIndex:0]

//cell customization

return cell
}

Why you don't want to reuse the cell ? any specific reason ? It would be more memory consuming as each time it will create a new cell. You can use code as follow to reuse if you need,

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

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
    cell = [[[NSBundle mainBundle] loadNibNamed:@"NibFile" owner:self options:nil] objectAtIndex:0]

//cell customization

return cell
}
like image 98
Janak Nirmal Avatar answered Feb 16 '23 04:02

Janak Nirmal