i want view data from json in table view - ios

1-connection
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
_arrayOfData = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingJSON5Allowed error:nil];
}
2 - Table view data source`enter code here`
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _arrayOfData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
_dObj = [_arrayOfData objectAtIndex:indexPath.row];
cell.textLabel.text =[_dObj objectForKey:#"title"];
return cell;
}

Related

Put JSON elements in TableView as Sections Title and Cell title

Below is JSON, and need to put it in table view such that "subitemname" becomes the section title and "subtosubitemname" becomes the title for cell.
I am trying it but didn't get successful.
Title for section is in place but title for rows is not retrieved.
here is the code I tried.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlString = [NSString stringWithFormat:#"URL"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"GET"];
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSArray *DATAA=[jsonDict objectForKey:#"Menu"];
subid=[DATAA valueForKey:#"subitemid"];
subname=[DATAA valueForKey:#"subitemname"];
NSArray *sub= [DATAA valueForKey:#"Submenu"];
_SubItemName=[sub valueForKey:#"subtosubitemname"];
_SubItemId=[sub valueForKey:#"subtosubitemid"];
}
#pragma mark - Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ return [subname objectAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [subid count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_SubItemId count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell=[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSArray *subn=[_SubItemName objectAtIndex:indexPath.section];
cell.textLabel.text=[subn objectAtIndex:indexPath.row];
return cell;
}
where subname is an array having "subitemname" objects and _SubItemName is an array having "subtosubitemname" objects.
{ "Menu":
[{"subitemid":1,
"itemid":1,
"subitemname":"TELEVISIONS",
"ordernum":1,
"Submenu":
[{
"subitemid":1,
"subtosubitemid":1,
"itemid":1,
"subtosubitemname":"FULL HD TV"},
{
"subitemid":1,
"subtosubitemid":2,
"itemid":1,
"subtosubitemname":"SMART TV"
}
]
},
{
"subitemid":2,
"itemid":1,
"subitemname":"AUDIO & VIDEO",
"ordernum":2,
"Submenu":
[{
"subitemid":2,
"subtosubitemid":3,
"itemid":1,
"subtosubitemname":"HOME AUDIO SYSTEMS"
},
{
"subitemid":2,
"subtosubitemid":4,
"itemid":1,
"subtosubitemname":"DTH SERVICES"
},
{
"subitemid":2,
"subtosubitemid":5,
"itemid":1,
"subtosubitemname":"AUDIO & VIDEO ACCESSORIES"
},
{
"subitemid":2,
"subtosubitemid":11,
"itemid":1,
"subtosubitemname":"PROJECTORS"
}
]
},
{
"subitemid":3,
"itemid":1,
"subitemname":"LARGE APPLIANCES",
"ordernum":3,
"Submenu":
[
{
"subitemid":3,
"subtosubitemid":14,
"itemid":1,
"subtosubitemname":"WASHING MACHINES & DRYERS"
},
{
"subitemid":3,
"subtosubitemid":16,
"itemid":1,
"subtosubitemname":"AIR CONDITIONERS"
},
{
"subitemid":3,
"subtosubitemid":18,
"itemid":1,
"subtosubitemname":"REFRIGERATORS"
},
{
"subitemid":3,
"subtosubitemid":19,
"itemid":1,
"subtosubitemname":"INVERTERS & BATTERIES"
}]}]}
NSMutableArray *menuData = [jsonDict objectForKey:#"Menu"];
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [[menuData objectAtIndex:section] objectForKey:#"subitemname"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [menuData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *subMenuData = [[menuData objectAtIndex:section] objectForKey:#"Submenu"];
return [subMenuData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell=[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDictionary *cellData = [[[menuData objectAtIndex:section] objectForKey:#"Submenu"] objectAtIndex:indexPath.row];
cell.textLabel.text=[cellData objectForKey:#"subtosubitemname"];
return cell;
}
Hope this work for you.
Try this ... Just one array is enough.. menu = [jsonDict objectForKey:#"Menu"];
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return menu[indexPath.section][#"subitemname"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [menu count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *submenu = menu[indexPath.section][#"Submenu"];
return [submenu count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell=[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSArray *submenu = menu[indexPath.section][#"Submenu"];
cell.textLabel.text = submenu[indexPath.row][#"subtosu‌​bitemname]";
return cell;
}

IOS,Tableview Objective C,

There is 5 elements in an array , in that 2 elements have data and another elements are blank and in tableview it show both element but when i scroll the app crash.i have to show menu_name in the section and its working but when i scroll tableview the app crash because, so can any one give me the solution?
Here is the Response which i have to show in tableview
"status":"true",
"message":"food point menu available",
"data":[
{
"menu_id":"9",
"menu_name":"Morning Menu",
"food_list":[
{
"food_name":"Brigadeiros",
"foodtype":"Pastas",
"food_per_person":"20",
"food_bonus":"packing"
},
{
"food_name":"Cachaca",
"foodtype":"Pastas",
"food_per_person":"30",
"food_bonus":"packing"
}
]
},
{
"menu_id":"10",
"menu_name":"Afternoon Menu",
"food_list":[
{
"food_name":"Quindim",
"foodtype":"Pastas",
"food_per_person":"30",
"food_bonus":"packing"
},
{
"food_name":"Cachaca",
"foodtype":"Pastas",
"food_per_person":"30",
"food_bonus":"packing"
}
]
},
{
"menu_id":"12",
"menu_name":"Evening Menu",
"food_list":""
},
{
"menu_id":"17",
"menu_name":"MenuSegundaFeira",
"food_list":""
},
{
"menu_id":"18",
"menu_name":"MenuTercaFeira",
"food_list":""
}
]
}
And this is the code which i have written in my app
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([FinalArrayCategory count]>0) {
return [[[FinalArrayCategory objectAtIndex:0] objectForKey:#"food_list"] count];
}
else
{
return 0;
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([FinalArrayCategory count]>0) {
return [[FinalArrayCategory valueForKey:#"menu_name"] count];
}
else
{
return 0;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
sectionName =[[FinalArrayCategory valueForKey:#"menu_name"] objectAtIndex:section];
return sectionName;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.lblpice.text=[[[[FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:#"food_list"] valueForKey:#"food_name"]objectAtIndex:indexPath.row];
// cell.lblType.text=[[FinalArrayCategory valueForKey:#"category_name"] objectAtIndex:indexPath.row];
// cell.lblBonus.text=[[FinalArrayCategory valueForKey:#"food_bonus"] objectAtIndex:indexPath.section];
// cell.lblFoodName.text=[[FinalArrayCategory valueForKey:#"menu_name"] objectAtIndex:indexPath.section];
//
// NSString *pickURL=[[FinalArrayCategory valueForKey:#"food_image"] objectAtIndex:indexPath.section];
// [cell.lblImage sd_setImageWithURL:[NSURL URLWithString:pickURL] placeholderImage:[UIImage imageNamed:#"itemplaceholder.jpg"] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// }];
return cell;
}
return [[[FinalArrayCategory objectAtIndex:0] objectForKey:#"food_list"] count];
change objectAtIndext from 0 to IndexPath.section
Please try this code..
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[FinalArrayCategory objectAtIndex:section] objectForKey:#"food_list"] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [FinalArrayCategory count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
sectionName =[[FinalArrayCategory objectAtIndex:section] objectForKey:#"menu_name"];
return sectionName;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([[[FinalArrayCategory objectAtIndex:indexPath.section] objectForKey:#"food_list"] iskindOfClass:[NSArray class]]) {
NSDictionary *dictData=[[[FinalArrayCategory objectAtIndex:indexPath.section] objectForKey:#"food_list"] objectAtIndex:indexPath.row];
cell.lblpice.text=[dictData objectForKey:#"food_name"];
}else{
cell.lblpice.text=#"";
}
return cell;
}
And let me know its working or not.
Happy Coding!!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if([FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:#"food_list"].count>0){
NSString *foodName =[[[[FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:#"food_list"] valueForKey:#"food_name"]objectAtIndex:indexPath.row];
if(foodName){
cell.lblpice.text=foodName;
}
}else{
cell.lblpice.text=#"";
}
return cell;
}
please try this

How to move row to another Section while clicking on row?

I have Implemented multi select row but what i have to do is when we click on row it will move on another section , Here's my code what i have tried is ?
can anyone help me out from this ? Thanks in advance ...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //assuming you have only two sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return self.firstSectionDataSource.count;
else if (section == 1)
return self.dataArray.count;
else
return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here, Have to move row to another section while clcking ..
// other multiselect code
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure a cell to show the corresponding string from the array.
static NSString *kCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
#try {
NSMutableArray *sourceArray = nil;
if (sourceIndexPath.section == 0) {
sourceArray = self.firstSectionDataSource;
}
else if (sourceIndexPath.section == 1)
{
sourceArray = self.dataArray;
}
NSMutableArray *destinationArray = nil;
if (destinationIndexPath.section == 0) {
destinationArray = self.firstSectionDataSource;
}
else if (destinationIndexPath.section == 1)
{
destinationArray = self.dataArray;
}
NSString *stringToMov = [sourceArray objectAtIndex:sourceIndexPath.row];
[sourceArray removeObject:stringToMov];
[destinationArray insertObject:stringToMov atIndex:destinationIndexPath.row];
}
#catch (NSException *exception) {
NSLog(#"exception = %# \n reason = %#",exception,exception.reason);
}
}
In cellForRowAtIndexPath
[cell setShowsReorderControl:YES];
And you need to implement the following UITableView delegate and data source methods properly
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //assuming you have only two sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return firstSectionDataSource.count;
else if (section == 1)
return secondSectionDataSource.count;
else
return 0;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
#try {
NSMutableArray *sourceArray = nil;
if (sourceIndexPath.section == 0) {
sourceArray = firstSectionDataSource;
}
else if (sourceIndexPath.section == 1)
{
sourceArray = secondSectionDataSource;
}
NSMutableArray *destinationArray = nil;
if (destinationIndexPath.section == 0) {
destinationArray = firstSectionDataSource;
}
else if (destinationIndexPath.section == 1)
{
destinationArray = secondSectionDataSource;
}
NSString *stringToMov = [[sourceArray objectAtIndex:sourceIndexPath.row] retain];
[sourceArray removeObject:stringToMov];
[destinationArray insertObject:stringToMov atIndex:destinationIndexPath.row];
[stringToMov release];
}
#catch (NSException *exception) {
NSLog(#"exception = %# \n reason = %#",exception,exception.reason);
}
}
You can do the next:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
// other multiselect code
}
Try this -
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:[NSArray arrayWithObjects:#"a",#"b",#"c", nil],[NSArray arrayWithObjects:#"d",#"e",#"f", nil], nil] forKeys:[NSArray arrayWithObjects:#"firstSection",#"secondsection", nil]];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[dict allKeys] count]; //assuming you have only two sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[dict objectForKey:[dict allKeys][section]] count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *arr = [dict objectForKey:[dict allKeys][indexPath.section]];
NSString *str = arr[indexPath.row];
if (indexPath.row!=0) {
[arr removeObject:str];
}
NSMutableArray *arr1 = [dict objectForKey:[dict allKeys][0]];
[arr1 addObject:str];
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *arr = [dict objectForKey:[dict allKeys][indexPath.section]];
NSString *str = arr[indexPath.row];
static NSString *kCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
cell.textLabel.text = str;
return cell;
}

Two tableView in One Controller

Want to use two tableView in one viewController but it give me error like:
this class is not key value coding-compliant for the key tableView.
I have two tableView 1.)tableViewTwo and 2.)tableViewThree but not wokring.
but when i used the name tableView then data shows on the first tableView.
and how can i use the different custom cell on both tableView. (CustomerListCell for (First TableView) and CustomerListCellThree for (Second TableView)
Can anyone please suggest me the best way to do this. I have search a lot and trying many suggestion which i found on internet and stackoverflow.
Thanks In Advanced.
- (void)viewDidLoad
{
NSURLRequest *requestTwo=[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://xyzt.com?key=init"]];
responseData=[[NSMutableData alloc]init];
urlConnection=[[NSURLConnection alloc]initWithRequest:requestTwo delegate:self];
NSURLRequest *requestThree=[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://xyzt.com?key=init"]];
responseDataThree=[[NSMutableData alloc]init];
urlConnectionThree=[[NSURLConnection alloc]initWithRequest:requestThree delegate:self];
}
#pragma mark - Connection Details
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
if (connection == urlConnection) {
NSLog(#"Error");
}
else if (connection == urlConnectionThree){
NSLog(#"Error");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
if (connection == urlConnection) {
[responseData setLength:0];
}
else if (connection == urlConnectionThree){
[responseDataThree setLength:0];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (connection == urlConnection) {
[responseData appendData:data];
}
else if (connection == urlConnectionThree) {
[responseDataThree appendData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection == urlConnection) {
NSError *error=nil;
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
self.totalData=[dic objectForKey:#"data"];
totalTitle=[[NSMutableArray alloc]init];
totalImage=[[NSMutableArray alloc]init];
totalIdWorks=[[NSMutableArray alloc]init];
[_tableViewTwo reloadData];
}
else if (connection == urlConnectionThree){
NSError *error=nil;
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseDataThree options:NSJSONReadingMutableContainers error:&error];
self.totalDataThree=[dic objectForKey:#"data"];
totalTitleThree=[[NSMutableArray alloc]init];
totalImageThree=[[NSMutableArray alloc]init];
totalIdWorksThree=[[NSMutableArray alloc]init];
[_tableViewThree reloadData];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableViewTwo) {
return [totalData count];
}
else if (tableView == self.tableViewThree){
return [totalDataThree count];
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustomerListCell";
CustomerListCell *cell = (CustomerListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomerListCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
Try this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40.0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == tableViewFirst)
return arrfirst.count;
else if(tableView==tableViewSecond)
return arrsecond.count;
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == tableViewfirst)
{
// First tableViewcell
return cell;
}
else if (tableView == tableViewsecond)
{
//Second tableViewCell
return cell;
}
return nil;
}
No matter how much table view you have in a single viewController. As you said you have two tableView tableViewTwo & tableViewThree. So you have to create two array for two datasource. Then set the delegate of both table view.
[tableViewTwo setDelegate:self];
[tableViewThree setDelegate:self];
Now when you reload your table views
[tableViewTwo reloadData];
[tableViewThree reloadData];
for both table view the same delegate will called one after another. As you already put the condition on
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
do the same thing for all the delegate method you have implemented and you want to make differ for different table view. One thing I want to include use ([tableView isEqual:self.tableViewTwo]) instead (tableView == self.tableViewTwo)
i think this is helpful you for understand #Tapas Pal said set delegates for both tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *customCell;
UITableViewCell *customTabelCell;
if(tableView == self.tableViewTwo){
customCell=#"CustomerListCellTwo";
customTabelCell=CustomerListTabelCellTwo;
}else{
customCell=#"CustomerListCellThree";
customTabelCell=CustomerListTabelCellThree;
}
static NSString *simpleTableIdentifier = customCell;
customTabelCell *cell = (customTabelCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:customCell owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}

Why won't my JSON array display in a UITableView object?

Here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.domain.com/venues.php"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *error = nil;
NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];
NSLog(#"%#", responseArray); // This logs fine
}
#synthesize responseArray;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1; // temporary replacement for return [responseArray count] for testing purposes
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[responseArray objectAtIndex:indexPath.row];
return cell;
}
For comparison, the following code does work:
- (void)viewDidLoad
{
[super viewDidLoad];
self.colors= [[NSArray alloc] initWithObjects: #"Red", #"Yellow", #"Green", #"Blue", #"Purple", nil];
NSLog(#"%#", self.colors); // logs fine
}
#synthesize colors;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.colors count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[self.colors objectAtIndex:indexPath.row];
return cell;
}
Update
My cellForRowAtIndexPath now looks like this, but I'm still not getting any results. Is there a problem with my JSON?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[[self.responseArray objectAtIndex:indexPath.row]objectForKey:#"name"];
return cell;
}
JSON:
[{"name":"venue 1"},{"name":"venue 2"},{"name":"venue 3"}]
#interface YourTableViewController ()<BViewControllerDelegate>
#property (strong, nonatomic) NSArray *responseArray;
#end
#implementation yourTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.responseArray = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.responseArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[[self.responseArray objectAtIndex:indexPath.row]objectForKey:#"yourKey"];
return cell;
}

Resources