禁用小型固定大小的UITableView的单元重用

我有一个小的,固定大小的表,我想要将UITableView完全加载到内存中,并且如果它们从视图中滚出,则不会重用单元。 我如何做到这一点?

我没有使用UITableViewController; 只是一个简单的UIViewController实现适当的协议(UITableViewDataSource和UITableViewDelegate)。

在行中为重用标识符设置nil

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

或者只是删除该行并添加,

 UITableViewCell *cell = nil; 

只是不要实现方法UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SomeID"]; 你的单元格都不会被重用。 每次请求一个单元格时,都会创build一个新的单元格并对其进行configuration。

您应该在方法initWithStyle:reuseIdentifier:传递nil initWithStyle:reuseIdentifier:如果您不想重用单元,但要记住性能。 只要它是好的,你应该可以通过nil

前三个答案是完全正确的,你只需要确保你不要在UITableView上调用dequeueReusableCellWithIdentifier函数。 除此之外,您可以简单地分配代码中的单元格。 首先你需要一个实例variables来存储指向你的单元格的指针(我假设你使用了一个普通的视图控制器):

 @interface MyViewController @property (nonatomic, strong) NSArray* myTableViewCells; @end 

然后你可以通过重写它的getter来延迟实例化这个数组:

 - (NSArray *)myTableViewCells { if (!_myTableViewCells) { _myTableViewCells = @[ [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil], [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ]; } return _myTableViewCells; } 

如果你喜欢,可以在数组中添加更多的单元格,或者使用NSMutableArray 。 现在你所要做的就是把这个数组连接到适当的UITableViewDataSource方法。

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.myTableViewCells.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = self.myTableViewCells[indexPath.row]; // // Add your own modifications // return cell; } 

这使得代码更干净,不太容易出现内存泄漏( 程序结束时静态variables会被释放,那么为什么我们要保留表格视图单元格,如果显示它们的视图控制器已经不存在了? )。

新的单元格的添加也更加容易(不需要切换或者如果语句需要)并且代码更好地结构化。

EDITED

有时候你需要一些单元格是静态的,比如你需要第一个单元格下载有下载进度条的单元格。 和其他单元等待下载单元。 在这种情况下,应该可以访问第一个单元格来暂停和恢复函数(在tableView:cellForRowAtIndexPath之外)。

你可以尝试创build这样的静态单元格:

第一:UITableViewCell的子类,创build你自己的单元格(这是一个选项)

第二:在视图控制器中创build静态单元

 static YourSubclassedTableViewCell *yourCell_0; static YourSubclassedTableViewCell *yourCell_1; static YourSubclassedTableViewCell *yourCell_2; static YourSubclassedTableViewCell *yourCell_3; 

第三:viewDidLoad中的Init单元(viewDidLoad是放入init代码的好select)

 - (void)viewDidLoad { yourCell_0 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; yourCell_1 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; yourCell_2 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; yourCell_3 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; // or simply yourCell_0 = [[YourSubclassedTableViewCell alloc] init]; yourCell_1 = [[YourSubclassedTableViewCell alloc] init]; yourCell_2 = [[YourSubclassedTableViewCell alloc] init]; yourCell_3 = [[YourSubclassedTableViewCell alloc] init]; } 

第四:称重传感器

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.row) { case 0: yourCell_0.textLabel.text = @"1st Row"; return yourCell_0; case 1: yourCell_1.textLabel.text = @"2nd Row"; return yourCell_1; case 2: yourCell_2.textLabel.text = @"3rd Row"; return yourCell_2; case 3: yourCell_3.textLabel.text = @"4th Row"; return yourCell_3; default: defaultCell....(ignore) return defaultCell; } } 

**如上所述,单元格被创build一次,并可以在tableView之外访问:cellForRowAtIndexPath:

你也可以声明单元格为@property,使其他类可以访问。

只需要分配一个新的单元来代替出队,不需要做任何以上的事情。 性能影响可以忽略小tableView(<100单元格)。

快速示例3

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .default, reuseIdentifier:"Cell") return cell } 

干杯