objective-c - tableviewcell creation
仅有的幸福
仅有的幸福 2017-05-02 09:25:41
0
1
495

if (!cell) {

cell = [[AddressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:addressIdentifier];

}
Why do you need to add a judgment, write this, when will this judgment be made

仅有的幸福
仅有的幸福

reply all(1)
刘奇

If you don’t judge, a new one will be created every time, because you have one here alloc.
Usually there is another line before this code:

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

This line is to get the reused cell. If it can be retrieved, the cell has memory and can be reused directly. If it cannot be retrieved, you have to create it yourself, which is the code you posted.
In addition, you can also use the registration method:

-(void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
-(void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

If you register, you don’t need to judge. Generally speaking, it is used like this:

[self.tableView registerNib:[UINib nibWithNibClass:className] forCellReuseIdentifier:reuseId];
//或者
[self.tableView registerClass:className forCellReuseIdentifier:reuseId];

Then, the tableView:cellForRowAtIndexPath: method does not require additional judgment, and can be taken directly:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SomeIdentifier"];
    //do something
    ...
    
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template