I started to use IOS and the Graph Api some weeks ago. I have problems to fill a UITableView with the albums data that I retrieve from facebook. I make my appdelegete my uitabledatasource and uitabledelegate too I make the following request after the user is logged:
NSString* fql = [NSString stringWithString:#"select object_id, cover_object_id, name, description from album where can_upload=1 and owner=me ()"];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:fql forKey:#"query"];
[facebook requestWithMethodName:#"fql.query" andParams:params andHttpMethod:#"GET" andDelegate:self];
[theTable reloadData];
And I implement the following to the table:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.resultData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row]; cell.textLabel.text = [[self.resultData objectAtIndex:row] valueForKey:#"name"];
NSLog(#"the name %#", [[self.resultData objectAtIndex:row] valueForKey:#"name"]);
return cell;
}
And a manage the result like this:
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]]) {
resultData=result;
[theTable reloadData];
}
NSLog(#"Result of API call: %#", result);
}
I get a correct result of the query but the table never reload the data so it is always empty
First of all, you need to retain resultData in request:didLoad: (use the setter method!).
Secondly, you should check if the table does not try reload the data at all or if something in your table view data source methods goes wrong. If the former, it is likely that theTable is actually nil in request:didLoad: so that the reloadData call has no effect. Check that with the debugger.
If that's not the case, we need more information. Go through the code with the debugger and check for each of the table view data source methods if they get called and where in that flow the processing goes wrong.
Related
I have a array which stores all the contacts from the phone. I have a UITableView in which i want to display all the contacts. I was having trouble displaying them in the table view o i created a new array, and used the below code to loop through the contacts array and fetch strings from it and store them in the new array (contactsNew)
for (CNContact *contact in contacts)
{
NSString *string = [formatter stringFromContact:contact];
[contactsNew addObject:string];
NSLog(#"contact = %#", string);
}
The issue is all the contacts are displayed in the log, but i am still not able to display the contacts in table view.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [contactsNew count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row];
return cell;
}
I have checked the values of 'string' variable and the number and content being added to the contactsNew array, and everything looks fine.
Also can i use something else in place of:
cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row];
So that i can directly use the contacts array instead of creating a new array. Thanks in advance.
You need to reload tableView after creating array so use [tableView reloadData] after for loop
for (CNContact *contact in contacts)
{
NSString *string = [formatter stringFromContact:contact];
[contactsNew addObject:string];
NSLog(#"contact = %#", string);
}
[tableview reloadData]; //Add this line
Hope this will works.
you need to reload tableview after some delay.
use like this
for (CNContact *contact in contacts)
{
NSString *string = [formatter stringFromContact:contact];
[contactsNew addObject:string];
NSLog(#"contact = %#", string);
}
// add after Delay 0.5 if its not worked then 1.0
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[tableview reloadData];
});
Try this
for (CNContact *contact in contacts)
{
NSString *string = [formatter stringFromContact:contact];
[contactsNew addObject:string];
NSLog(#"contact = %#", string);
}
[tableview reloadData];
you have to reload your table on main thread after you fetch the contacts... like:- dispatch_async(dispatch_get_main_queue(), ^{ [_table reloadData]; });
...and for your table view write
cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row]valueForKey:#"keys";
...provide the key here.
Here are few points you need to check to resolve your issue :
First thing you need to check is weather tableview methods called once after you added values to new Array ? If no than try reload table view after you are adding values to new array.
If it calls correctly after adding values to new array try to NSLog values in cellForRowAtIndexPath method
Hope it will help you.
Hi I am trying to display two dimensional array like in this image but not work my code please help me and my json respone is in image which i want to display in UITableView
-(void)getCategories
{
Service *srv=[[Service alloc]init];
NSString *str=#"http://streamtvbox.com/site/api/matrix/";//?country_code=91&phone=9173140993&fingerprint=2222222222";
NSString *method=#"channels";
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[srv postToURL:str withMethod:method andParams:dict completion:^(BOOL success, NSDictionary *responseObj)
{
NSLog(#"%#",responseObj);
arrayCategories = [responseObj valueForKey:#"categories"];
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayCategories.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ChannelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [[arrayCategories objectAtIndex:indexPath.row]valueForKey:#"categories"];
return cell;
}
Hope it will help you,
You are doing some minor mistake, do something as below,
arrayCategories = [responseObj valueForKey:#"categories"]; //Which is already there in your code....
And while fetching value in your textLabel, write as below
cell.textLabel.text = [[[arrayCategories objectAtIndex:indexPath.row] componentsSeparatedByString:#"="] objectAtIndex:1];
Do not write here valueForKey "categories".
Let me know if you still face any issue.
I got it the issue is in your response.In categories key you are not getting array , you are getting object.Paste exact log NSLog(#"%#",responseObj); that you get in your success block so that i can help you to sort it out.
I'm building an app that retrieves items from a Parse database and inserts them into a UITableView. In my Parse database, each item has its own date (i.e. film dates). Now this is the part I’m having trouble with:
I would like to sort the items by their own date, with the date for that item being in the section header. I would also like to show only the items whose date is from the current date and onwards. Please also know that I’m using the latest version of iOS and using Objective-C. I have exhausted other sources but have yet to find exactly what I need. Any guidance will be greatly appreciated!
Right now I have this in my ShowsTableViewController.m:
My getShows method:
-(void)getShows{
PFQuery *retrieveShows = [PFQuery queryWithClassName:#"shows"];
[retrieveShows findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError
*error) {
//NSLog(#"%#", objects);
if (!error)
{
_showsArray = [[NSArray alloc] initWithArray:objects];
}
[showsTableView reloadData];
}];
}
And this is how I'm currently populating the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ShowsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"showsCell" forIndexPath:indexPath];
PFObject *tempObject = [_showsArray objectAtIndex:indexPath.row];
cell.cellTitle.text = [tempObject objectForKey:#"title];
return cell;
}
With you require, I think you have to sort your data and put it in to a Array. And this Array will hold list of Dictionary. Each Dictionary will have two key (first key is title: and value of it is title you want show to header, second key is data: and value of it is array item in seciton)
And now you will implement UITableViewDataSource for it:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.listData count];
}
for each section you can implement same like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *data = [[self.listData objectAtIndex:section] objectForKey:#"data"];
}
And show header:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.listData objectAtIndex:section] objectForKey:#"title"];
}
And show to cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ShowsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"showsCell" forIndexPath:indexPath];
PFObject *tempObject = [[self.listData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.cellTitle.text = [tempObject objectForKey:#"title];
return cell;
}
You can do this way to achieve what you want.
I am new to iOS. i have to parse following JSON and display it to UITableViewCell. individual array is appearing in cell when i parse and append country item only. but all arrays for ex. rank ,country , population , flag are not appearing in cell.
how to add all rank , country , population , flag in array and put all them in cell. i have taken all them into string and then into array. and whole array i appended to main array.
following is JSON -
http://www.androidbegin.com/tutorial/jsonparsetutorial.txt
code
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSArray *arrayWorldPopulation = [allDataDictionary objectForKey:#"worldpopulation"];
for (NSDictionary *diction in arrayWorldPopulation)
{
NSString *country = [diction objectForKey:#"country"];
NSString *population = [diction objectForKey:#"population"];
NSString *flag = [diction objectForKey:#"flag"];
NSArray *temparray = [[NSArray alloc] initWithObjects:rank,country,population,flag, nil];
[array addObject:temparray];
}
[maintableView reloadData];
}
At a minimum, you need to implement tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath in your view controller. This tells the table how many rows to expect and what each row in the table looks like. The simple code below assumes you have an array of strings and are just displaying one string per cell and should get you started. Your specific situation sounds like it may require a custom cell design. This tutorial describes how to do this in a storyboard with a custom cell class.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count]; //tell the UITableView how many are items are in the array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
//stuff to make sure iOS is reusing cells rather than creating new ones
static NSString *MyIdentifier = #"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
NSString *itemFromArray = [array objectAtIndex:indexPath.row]; //get the item for that cell
cell.textLabel.text = itemFromArray; set the cell to display the text
return cell;
}
I have a split view controller for iPad with a drill-down table on the left. I am able to populate my first table view and when I click on a cell this takes me to my second table view. I am able to see a count of records returned and the actual data I expect to see in the table view output in the command widow with the NSLog command. What I don't see is the actual data in the table view. Instead I see UITableViewCellAccessoryDisclosureIndicator for each row that is returned but no actual data.
I am using xib files and I have created this file for my products I want displayed in the drill-down. In my Product.xib file I have the File's Owner Outlets as productsTableView linked to products (my UITableView control) and view linked to View. Referencing Outlets for the View have dataSource linked to products, delegate linked to products and finally view linked to File's Owner.
Am I missing something here? Like I said I get all the data back it just isn't binding to the grid.
#interface ProductViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *listOfItems;
NSMutableArray *dataArray;
IBOutlet UITableView *productsTableView;
}
#property(nonatomic, strong) IBOutlet UITableView *productsTableView;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Set up the cell...
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = cellValue;
return cell;
}
In the interest of being thorough I will post this as well to show where I am getting my data back and how I am doing this...
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *error = nil;
// Get the JSON data from the website
id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
if ([result isKindOfClass:[NSArray class]]) {
for (NSArray *item in result) {
NSArray *products = [item valueForKey:#"ProductDescription"];
[dataArray addObject:products];
[listOfItems addObject:products];
}
}
else {
NSDictionary *jsonDictionary = (NSDictionary *)result;
for(NSDictionary *item in jsonDictionary)
NSLog(#"Item: %#", item);
}
[self performSelector:(#selector(refreshDisplay:)) withObject:(self.productsTableView) afterDelay:1.0];
NSLog(#"Finished");
}
My NSLog is here:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Dictionary: %#", dataArray);
return [dataArray count];
}
Simple mixup...
NSString *cellValue = [dataArray objectAtIndex:indexPath.row];
instead of
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
You are adding products (which is an NSArray) to listOfItems. Later in cellForRowAtIndexPath, you are saying cellValue (NSString) = [listOfItems objectAtIndex:indexPath.row] (which is potentially an `NSArray'). How would that work?