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

时光总嘲笑我的痴心妄想 提交于 2019-12-07 03:43:27

问题


I have 5 cells in a UITableView. Each has a UITextField as a subview, where the user will input data. 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?


回答1:


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. 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;

After that You need to replace textField value in below two methods.

-(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. So that whenever user scroll tableview it set previous value from temp array. 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.




回答2:


You can give each cell a unique ReuseIdentifier, maybe by appending the indexPath.row to the name. If you have only 5 cells, this will probably be fine, but you're losing one of the main benefits of a UITableView. In this case, you may want to use a UIScrollView instead.




回答3:


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.




回答4:


If you open up Apple's Recipes sample application, you will see how Apple uses a xib file to load UITableViewCells.

In the IngredientDetailViewController file:

@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.

Note: you will probably need to store the cells as strong properties (rather than assigning them).



来源:https://stackoverflow.com/questions/6516065/uitableview-dont-reuse-cells-good-case-to-do-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!