IOS Learning -- the difference between UITableViewCell and iosuitableviewcell
UITableView is used in the development process today. When setting the cell, I found that there are two ways to set the reuse of UITableViewCell, at the beginning, I was not quite clear about the differences between the two methods. The specific implementation code is as follows when I use method 2 for reuse. CJMeetingReplyBasicCell is my custom UITableViewCell type, however, when running, CJMeetingReplyBasicCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "BasicCell" forIndexPath: indexPath] is called every time. The system crashes and finds the cause, after confirming that the problem is not your own code, start to understand the differences between the two reuse methods. So what is the difference between the two methods that reuse UITableViewCell?
// Method 1 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; // method 2 UITableViewCell * cell = [tableView metadata: CellIdentifier forIndexPath: indexPath];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath]; if (!cell) { cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"]; } return cell;}
1. Official Website documentation explanation
First, let's take a look at the UITableView In the iOS source code. h. The two are explained as follows. We can see that method 2 is the new method launched in iOS 6.0. In the explanation of method 2, we note that the red part meansAssume that we have registered an identifier.Here we guess we need to register the identifier.
//UITableView.h- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
Next, find the official document,Https://developer.apple.com/documentation/uikit/uitableview/1614878-dequeuereusablecellwithidentifie? Language = objcThe official documentation has two points to note about the interpretation of method 2. The first is the return value. For example, this method always returns a valid UITableViewCell, this is one of the differences with method 1.
The second thing to note is that there is an Important prompt below this page. This prompt shows the correct use of method 2. It is explained that we should first register our custom or use the nib class and identifier, and then use method 2 for reuse. So now the cause of our crash is clear, and the problem is that we didn't register our custom classes and identifiers first.
The general usage of these two methods is summarized below.
First of all, for method 1, the usage method is very simple, without other definitions and registration, the Code is as follows.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell"]; if (!cell) { cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"]; } return cell;}
Next, let's simply use method 2 for reuse. The specific steps are as follows.
// First, we need to register the defined cell class and reuse identifier self When configuring tableview. tableView. backgroundColor = xxxx; [self. tableView registerClass: [MeetingReplyBasicCell class] forCellReuseIdentifier: @ "BasicCell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath]; return cell;}