InitWithFrame vs initWithStyle

I want to update my TableView from a deprecated initWithFrame:reuseIdentifier:

My table view uses custom cells.

It is said throughout that it uses initWithStyle: and that it does not change the behavior or cell in any way from initWithFrame:CGRectZero reuseIdentifier:

But when I build using initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cells become empty (i.e. our custom cell is not working (because it is initialized by some style?)).

After the cell is initialized (if it has not been deleted), we set the texts in the cell. But they are not installed when I use initWithStyle:reuseIdentifier: but it works with initWithFrame:CGRectZero . None of the code changes except the init method used ( initWithStyle ).

These lines are placed after the creation (or use) of the cell:

 cell.newsItemNameLabel.text = @"test"; NSLog(@"NewsItemName: %@",cell.newsItemNameLabel.text); 

Results in "NewsItemName: (null)"

Does anyone have an idea? What is the difference between the two?

thanks

+4
source share
1 answer

Your implementation of cellForRowAtIndexPath should look something like this:

 - (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; CustomCell *cell = (CustomCell *)(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell. cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail"); return cell; } 

where CustomCell is the name of your custom cell class. Note that this implementation uses ARC (Automatic Reference Counting). If you are not using this function, add an autorelease call to your cell distribution.

CustomCell initWithStyle implementation:

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { //do things } return self; } 
+3
source

Source: https://habr.com/ru/post/1397636/


All Articles