ios 禁用固定大小的小 UITableView 的单元格重用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6443533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable cell reuse for small fixed-size UITableView
提问by Sh'mu'el Cohen
I have a small, fixed-size table, and I want to load UITableView entirely into memory, and never reuse cells if they scroll out of view. How do I achieve this?
我有一个固定大小的小表,我想将 UITableView 完全加载到内存中,并且如果单元格滚动出视图,则永远不要重用它们。我如何实现这一目标?
I'm not using a UITableViewController; just a simple UIViewController that implements the proper protocols (UITableViewDataSource and UITableViewDelegate).
我没有使用 UITableViewController;只是一个简单的 UIViewController,它实现了正确的协议(UITableViewDataSource 和 UITableViewDelegate)。
回答by EmptyStack
Set nil
for reuse identifier in the line
设置nil
为在行中重用标识符
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
Or just remove the line and add,
或者只是删除该行并添加,
UITableViewCell *cell = nil;
回答by Joe
Just do not implement the method UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SomeID"];
and none of your cells will be reused. Each time it ask for a cell you create a new one and configure it.
只是不实施该方法UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SomeID"];
,您的任何单元格都不会被重用。每次它要求一个单元格时,您都会创建一个新单元格并对其进行配置。
回答by Deepak Danduprolu
You should pass nil
in the method initWithStyle:reuseIdentifier:
if you don't want to reuse cells but keep in mind the performance. As long as it is good, you should be ok passing nil
.
如果您不想重用单元格但要记住性能,则应传入nil
该方法initWithStyle:reuseIdentifier:
。只要是好的,你应该可以通过nil
。
回答by Legoless
First three answers are completely correct, you just need to make sure that you do not call dequeueReusableCellWithIdentifier
function on a UITableView
. In addition to this, you can simply allocate the cells in code. First you will need an instance variable that will store the pointers to your cells (I will assume you use a normal view controller):
首先三个答案是完全正确的,你只需要确保你不叫dequeueReusableCellWithIdentifier
上的功能UITableView
。除此之外,您可以简单地在代码中分配单元格。首先,您需要一个实例变量来存储指向单元格的指针(我假设您使用普通的视图控制器):
@interface MyViewController
@property (nonatomic, strong) NSArray* myTableViewCells;
@end
Then you can lazily instantiate this array, by overriding it's getter:
然后你可以懒惰地实例化这个数组,通过覆盖它的吸气剂:
- (NSArray *)myTableViewCells
{
if (!_myTableViewCells)
{
_myTableViewCells = @[
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]
];
}
return _myTableViewCells;
}
Add more cells into array if you like, or use NSMutableArray
. Now all you have to do is to wire up this array to proper UITableViewDataSource
methods.
如果您愿意,可以将更多单元格添加到数组中,或者使用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;
}
This makes for a much cleaner code, less prone to memory leaks (static variables get deallocated when program ends, so why are we keeping table view cells in memory if the view controller that is displaying them is already gone?).
这使得代码更简洁,更不容易发生内存泄漏(静态变量在程序结束时被释放,如果显示它们的视图控制器已经消失,为什么我们将表视图单元格保留在内存中?)。
The addition of new cells is also much easier (no switch or if statements required) and code is more nicely structured.
添加新单元格也更容易(不需要 switch 或 if 语句)并且代码结构更佳。
回答by adam88labs
Just alloc a new cell in place of dequeue no need to do any of the above. Performance implications are negligible for small tableView (< 100 cells).
只需分配一个新单元来代替出队,无需执行上述任何操作。对于小型 tableView(< 100 个单元格),性能影响可以忽略不计。
Example in swift 3
swift 3 中的示例
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier:"Cell")
return cell
}
Cheers
干杯
回答by Strong
EDITED
已编辑
Sometimes you need some cells to be static, for example, you need the first cell to be downloading cell which has download progress bar. and other cells to be waiting for download cells. In this case, the first cell should be accessible for pause and resume functions(outside tableView:cellForRowAtIndexPath:).
有时你需要一些单元格是静态的,例如,你需要第一个单元格是下载带有下载进度条的单元格。和其他单元格等待下载单元格。在这种情况下,暂停和恢复功能应该可以访问第一个单元格(在 tableView 之外:cellForRowAtIndexPath:)。
you could try to create static cells like this:
您可以尝试创建这样的静态单元格:
1st: subclass UITableViewCell, to create your own cell (this is a option)
第一:子类 UITableViewCell,创建你自己的单元格(这是一个选项)
2nd: crate static cell in your view controller
第二:在视图控制器中创建静态单元格
static YourSubclassedTableViewCell *yourCell_0;
static YourSubclassedTableViewCell *yourCell_1;
static YourSubclassedTableViewCell *yourCell_2;
static YourSubclassedTableViewCell *yourCell_3;
3rd: Init cells in viewDidLoad (viewDidLoad is a good choice to put init code)
第三:在viewDidLoad中初始化单元格(viewDidLoad是放置初始化代码的好选择)
- (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];
}
4th: Load cell
第四:称重传感器
- (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;
}
}
**As described above, cells are created once and can be accessed outside tableView:cellForRowAtIndexPath:
**如上所述,单元格创建一次,可以在 tableView:cellForRowAtIndexPath 之外访问:
You also could declare cells as @property to make it accessible for other class.
您还可以将单元格声明为 @property 以使其可供其他类访问。