I do not quite understand the meaning of reuseIdentifier in the tables, explain to someone in detail what it is and why it is? From what I read, I realized that this is an identifier of the nsstring type, assign it to an identifier, which we specify in the cell property in the storyboard or xib, and it is needed so that the cell is not re-created, such as by this identifier it is copied, or something like that .

    1 answer 1

    The idea is something like this: if you have 1000 cells in a table, say 1000 cells, they, of course, are not stored in memory. And exactly that quantity which you see on the screen is stored. To optimize the whole thing even more, and to bring the performance of the table to the maximum possible level, the mechanism of reuse of existing cells is used. That is, if you have 5 cells on the screen and you scroll down, then when the first cell disappears from the screen, it is not removed from memory, but, say, reused in place of the 7th cell. For this whole mechanism to work, you need to declare that all (or some) of the cells can be reused by declaring a common reuseIdentifier .

    Here is an example of how this looks.

     @interface ViewController () <UITableViewDataSource> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITableView *table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:table]; table.dataSource = self; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 100; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // здесь система проверит, есть ли в памяти не используемые ячейки данного типа UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell"]; if(cell == nil) { // если ячейки нет, то создать новую cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"]; cell.textLabel.text = @"new cell"; } else { // если ячейка есть, то изменить ее cell.textLabel.text = @"reused cell"; } return cell; }