how to display data from a NSMutablearray in a Table View - iphone

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.

Related

Displaying JSON Object in UITableView Cell - App Crashes

I'm loading a json file from my server which have several fields like id,category,value,price.i tried SBJSON library files. If i'm trying to display single content in a cell i'm using the following code
cell.textLabel.text=[[mutarray objectAtIndex:indexPath.row] valueForKey: #"id"];
but if i need to display all the field like id,category,value in each cell.i'm not aware of that.Kindly guide me..
here mutarray is my NSMutableArray which contains all elements of my json file..
You will need to put custom cells. Please check the link below and tell me if any doubts.
Putting Custom Cells in Table View
i done that using following code
cell.textlabel.text=[[NSString stringWithFormat:#"%#",[[mutarray objectAtIndex:indexPath.row] valueForKey: #"id"]];
The issue is i forgot to convert the JSON Object into NSSTring

How to fill UITableView after Initialization

I read tutorial here
http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/
which shows how to fill a UITableView at initialization. I can't find tut to do so after initialization (for example when User has clicked on a button).
Any suggestion ?
You would just update the data source and reload the table view.
Following that tutorial, you would do it like so:
- (IBAction)myButtonPressed:(id)sender
{
self.arrayData = [[NSArray alloc] initWithObjects:#"iPhone", #"iPod", #"MacBook", #"MacBook Pro", #"iMac"];
[self.tblSimpleTable reloadData]
}
This will tell the UITableView to reload the data source and adjust the amount of sections, rows etc.
Note that you might prefer to use a NSMutableArray instead.
[self.mutableArrayData addObject:#"iMac"];

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

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

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.