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

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?

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.

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.

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.

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

Related

how to display data from a NSMutablearray in a Table View

I'm trying to initialize a tableview with an image, name and surname of each user that I have stored in a NSMutableArray.
In this mutablearray I save a class that I created to store users data and its schema is as follows:
ResultSearchController{
NSInteger *userID;
NSString *userName;
NSString *userSurname;
NSString *userThumb;
}
I have read the documentation of TableView but I still have doubts and problems to display the data in TableView (seems rather complicated to use such objects). Could you help me to get started with this?
Thanks!!
I guess you have a Custom UITableViewCell for that.
Simply use in cellForRowAtIndexPath delegate of UITableView (to set the image):-
yourCustomCellObject.yourImageViewINCustCell.image=[UIImage imageNamed:[(yourClassToStoreData *)[yourNSMutableArray objectAtIndex:indexPath.row] userThumb]];
Similarly you can do in the same manner to set other control elements.
It's just an example of how you can do.I am assuming you have stored the data over some class objects and adding it to an NSMtableArray.

Correct way to get UITableViewCell's text

Simple question though, but what is the correct way to get the text off my UITableViewCell?
With cell a UITableViewCell:
I know off cell.text, which gives exactly what I want, but is deprecated in iOS 3.0.
The only alternative I could find was cell.textLabel but this gives me the same (or almost?) as if I only used cell. Which is obviously not what I want.
So how can I get the text on my cell (what the user reads)? Or is it okay if I leave it as cell.text / will Apple accept it?
Simply use cell.textLabel.text = #"My Text"; and NSString *mytext = cell.textLabel.text as long as you are targeting 3.0 or above.

One IBOutlet for two UITextFields

I have two UITextField one for Portrait and other for Lanscape View but I want that editing the first should reflect the changes to other text field too. I have to use separate views for both landscape and portrait. My last option is to make two outlets for them but I want to use only a single one.. Any help would be appreciated.
One option is to make use of IBOutletCollection. You can have one NSArray which you declare as an IBOutletCollection (instead of IBOutlet). In order to know what text field you are getting out of the array, you can set a tag on each one in IB and just pull from the array the text fields that match specific tags.
There is another workaround. You store the values from the text fields before you use the nib for the changed orientation like
NSString *firstText = firstTextField.text;
NSString *secondText = secondTextField.text;
then after exchanging the nibs use this to populate the text fields in the new nib like
firstTextField.text = firstText;
secondTextField.text = secondText;
You might implement appropriate method(s) in UITextFieldDelegate.....
Such as
//not real code here just off the top of my head
(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSString* updatedText = [textField text];
firstTextField.text = updatedText;
secondTextField.text = updatedText;
return YES;
}
u can use 2 diiferent xib files and do the same thing
To load a new xib file use the following code
[[NSBundle mainBundle] loadNibNamed:#"tests" owner:self options:nil];
u can still use the existing xib file by duplicating it and and loading it with the above code when the orientation changes.
There wont be much changes in the code as u can use the same iboutlets in both the orientations without even requiring to manually link them

Preload cells in tableView for use with mapView

I have a mapView and tableView, with each annotation in the mapView corresponding with a cell in the table. What I want to do is select the appropriate cell anytime an annotation is selected on the map.
As of right now, im creating an NSDictionary when the cells are created, which maps the row number to the annotationID. This works, but the problem is that the dictionary isnt completely populated until all the cells have been created, and all the cells arent created until youve scrolled all the way through the table. Thus, when the app starts for the first time, only the 4 annotations originally visible can be selected from the mapView.
So what im looking for is either a method to automatically populate my dictionary, or a better way of accomplishing what i need to do. Thanks a lot!
Well, first thoughts are that instead of populating the NSDictionary during scrolling, just populate it during viewDidLoad ..I do something similar where I populate all of my data into an NSDictionary and then use that to initialize/update the UI on the cells during scrolling for re-usable cells.
Also, by putting your data into an array of some kind, you can also map it to the cells. Just remember that arrays start at 0, so position in table = indexOfYourArray + 1
Simple example of loading an array stored in a plist into an NSArray in viewDidLoad
// Load the data
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:someArrayNameString ofType:#"plist"];
self.someArrayYouCreated = [NSArray arrayWithContentsOfFile:pathToFile];
Now that you just dumped a whole bunch of data into that array, you can populate it during scrolling in cellForRowAtIndexPath
NSDictionary *dataItem = [someArrayYouCreated objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.textColor = [UIColor colorWithRed:(58/255.f) green:(58/255.f) blue:(58/255.f) alpha:1.0];
label.text = [dataItem objectForKey:#"PersonName"];
With this example, you are just populating the cells from an array that you created in viewDidLoad and therefore all of your data is ready to use almost right away. If you have a ton of data, you could also throw up a progress circle to delay for a second until the array is finished loading.
Again, I don't quite understand how you are storing your data (since you have not said anything about that) so I can only speculate that this will work for you. Until you provide more detail, this is the best I can do.

how can I copy a string displayed in a UITableViewCell into another NSString?

I am writing a program, and one part of my program has a UItableView in which I set cell contents manually by using the following...
cell.textLabel.text = #"CELL CONTENTS HERE!";
How can I copy a string displayed in a particular TableView Cell into another NSString?
Try something like this:
NSString *anotherString = [NSString stringWithString:cell.textLabel.text];
Keep your #"CELL CONTENTS HERE!" string as a #property of your view controller. Then set the cell's text property to it:
cell.textLabel.text = cellContentsHereProperty;
inside the table view delegate method -tableView:cellForRowAtIndexPath:. You want to do this because you can only access cell.textLabel.text inside this method, or by calling the delegate method to retrieve the cell, which is awkward.
As a general concept, you want your view controller ("control") to keep the string value ("model") separate from how it is displayed ("view"). Keeping things compartmentalized lets you retrieve and change the data without worrying about how it is displayed.
This separation of responsibilities is called the MVC or Model-View-Control design pattern, which Apple subscribes to for iPhone application design.