UITableView dequeueReusableCellWithIdentifier Theory

前端 未结 3 1431
离开以前
离开以前 2020-11-28 05:55

When apple developed the UITableView for the first iPhone they had a problem in performance when scrolling through it. Then one clever engineer discovered that

3条回答
  •  独厮守ぢ
    2020-11-28 06:36

    The purpose of dequeueReusableCellWithIdentifier is to use less memory. if we use 100 cells in a tableView then need to create 100 cells every time.It reduce the app functionality and may cause crash. For that dequeueReusableCellWithIdentifier initialise the particular number of cells that we created and the cells will use again for further processing.

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *TableIdentifier = @"YourCellIdentifier";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
        }
    
        ExternalClassTableViewCell *myCell = [[ExternalClassTableViewCell alloc]init];
        myCell.MyCellText.text = [tableData objectAtIndex:indexPath.row];
        myCell.MyCellImage.backgroundColor = [UIColor blueColor];
    
        return cell;
    }
    

提交回复
热议问题