简体   繁体   English

UITableView 不要重复使用电池,这样做的好案例吗?

[英]UITableView don't reuse cells, good case to do this?

I have 5 cells in a UITableView.我在 UITableView 中有 5 个单元格。 Each has a UITextField as a subview, where the user will input data.每个都有一个 UITextField 作为子视图,用户将在其中输入数据。 If I DO use cell reuse, the textfield gets cleared if the cells are scrolled out of view.如果我确实使用单元格重用,那么如果单元格被滚动出视图,则文本字段将被清除。 I don't want to have to deal with this.我不想处理这个。 Is there a way to NOT reuse cells so that I don't have this issue, if so, how?有没有办法不重用单元格,这样我就没有这个问题,如果有,怎么办?

Is this a bad idea?这是一个坏主意吗?

I have same feature in one of my apps and I used below code to accomplish that and I never had this kind of problem.我在我的一个应用程序中具有相同的功能,我使用下面的代码来实现这一点,我从来没有遇到过这种问题。

First of all you need to store all your textField value temporary in Array.首先,您需要将所有 textField 值临时存储在 Array 中。 Make array like this.像这样制作数组。

arrTemp=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],nil];

Then Give all textField tag = indexPath.row;然后给所有 textField tag = indexPath.row;

After that You need to replace textField value in below two methods.之后,您需要在以下两种方法中替换 textField 值。

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}

At Last You need to set that value in cellForRowAtIndexPath datasource Method.最后,您需要在 cellForRowAtIndexPath 数据源方法中设置该值。 So that whenever user scroll tableview it set previous value from temp array.因此,每当用户滚动 tableview 时,它都会从临时数组中设置先前的值。 Like this.像这样。

cell.txtEntry.text = [arrTemp objectAtIndex:indexPath.row];

It might possible I forgot some of the code to paste here.我可能忘记了一些要粘贴在这里的代码。 So if you have any problem please let me know.因此,如果您有任何问题,请告诉我。

You can give each cell a unique ReuseIdentifier, maybe by appending the indexPath.row to the name.您可以给每个单元格一个唯一的 ReuseIdentifier,也许通过将 indexPath.row 附加到名称。 If you have only 5 cells, this will probably be fine, but you're losing one of the main benefits of a UITableView .如果您只有 5 个单元,这可能会很好,但是您将失去UITableView的主要好处之一。 In this case, you may want to use a UIScrollView instead.在这种情况下,您可能需要改用UIScrollView

If you open up Apple's Recipes sample application , you will see how Apple uses a xib file to load UITableViewCells.如果您打开Apple 的 Recipes 示例应用程序,您将看到 Apple 如何使用 xib 文件来加载 UITableViewCells。

In the IngredientDetailViewController file:在 IngredientDetailViewController 文件中:

@property (nonatomic, assign) IBOutlet EditingTableViewCell *editingTableViewCell;
// ...
[[NSBundle mainBundle] loadNibNamed:@"EditingTableViewCell" owner:self options:nil];
// this will cause the IBOutlet to be connected, and you can now use self.editingTableViewCell

Although it looks like they are reusing the cell, you could use the same method to load the 5 cells into 5 separate IBOutlets, and then in cellForRowAtIndexPath, you just return those 5 rather than calling the dequeue method.虽然看起来他们正在重用单元格,但您可以使用相同的方法将 5 个单元格加载到 5 个单独的 IBOutlets 中,然后在 cellForRowAtIndexPath 中,您只需返回这 5 个,而不是调用 dequeue 方法。

Note: you will probably need to store the cells as strong properties (rather than assign ing them).注意:您可能需要将单元格存储为strong属性(而不是assign它们)。

I would say 5 textview's is a perfect case for not queueing and de-queueing the cells, just create them all in view did load, store in an array and return as requested.我会说 5 个 textview 是一个完美的案例,不需要对单元进行排队和出队,只需在视图中创建它们并加载、存储在数组中并按要求返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM