UITableViewCell内存分配

vai*_*hav 1 memory-management objective-c uitableview

我不确定使用或不使用内部方法的tableView内存分配过程。reuseIdentifier statictableView delegate

假设我们有 20 个从方法 method 返回的数组,那么每次调用方法时,在方法之后将在内存中分配numberOfRowsInSection多少个数组。cellcellForRowAtIndexPath[UITableViewCell alloc] alloc

示例代码:

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 20;   // array count
}

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier]; 
        // how many cell will be alloc here
    }

// configure cell
}
Run Code Online (Sandbox Code Playgroud)

任何有用的澄清将不胜感激,感谢您的宝贵时间。

pkc*_*456 5

最初,当您的表视图首次在视图上可见时(假设它一次在屏幕上显示 5 个单元格),将分配 5 个单元格。当您向下滚动时,表视图不是实例化新的,而是使用dequeueReusableCellWithIdentifier. 所以这意味着 5 个单元格仅存在于内存中。

因此,AUITableView通常allocate只会有足够的UITableViewCell对象来显示表中可见的内容。

这种设计模式称为对象池