ios


my table view is created but array is not showing in table view,in console all the data are coming.please help me to solve json parsing


This is an response and I want to print this response values into the table view:
I am posting my tableview code please check is it right or wrong.
I am struggling over here for 2 days if any one know please reply me.
- (IBAction)search:(UIButton *)sender {
NSString *post =[[NSString alloc]initWithFormat:#"keyword=%#",[keyword text]];
NSLog(#"PostData: %#",post);
NSURL *url=[NSURL URLWithString:#"url"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
//NSError *error = [[NSError alloc] init];
NSError *error=nil;
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!urlData) {
NSLog(#"Error: %#", [error localizedDescription]);
//return NO;
}
NSLog(#"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"Response ==> %#", responseData);
NSError *error = nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers error:&error];
NSLog(#"json ==> %# ", dic);
name = [[NSMutableArray alloc]init];
username = [[NSMutableArray alloc]init];
for (id obj in dic) {
NSString *nameNameObj=[obj objectForKey:#"name"];
[name addObject:nameNameObj];
NSString *userNameObj=[obj objectForKey:#"username"];
[username addObject:userNameObj];
}
NSLog(#"name array is %#",name);
NSLog(#"username array is %#",username);
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [name count];
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [name count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MYCustomCell";
MyCustomCellTableViewCell *cell = (MyCustomCellTableViewCell *)
[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle]
loadNibNamed:#"MyCustomCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// apply setters for cell
cell.nameLbl.text=[name objectAtIndex:indexPath.row];
cell.usernameLbl.text=[username objectAtIndex:indexPath.row];
return cell;
}
[
{"id":60,"username":"test001","name":"name"},
{"id":61,"username":"test","name":""},
{"id":65,"username":"test002","name":"first"},
{"id":69,"username":"test003","name":"second"},
{"id":71,"username":"test004","name":"third"},
{"id":78,"username":"test005","name":"fourth"},
{"id":79,"username":"test006","name":"Name of the person"}
]
IGNORE THIS, AS YOu ARE NOT USING THIS
Check this 2 lines:
cell.nameLbl.text = [name objectAtIndex:indexPath.row];
cell.usernameLbl.text = [username objectAtIndex:indexPath.row];
name is an array, that contains a NSDictionary in each index. So if you want to access its values you have to do this:
cell.nameLbl.text = [[name objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.usernameLbl.text = [[username objectAtIndex:indexPath.row] objectForKey:#"username"]];
EDIT:
As you've edited your code, the modified answer will be :
- (IBAction)search:(UIButton *)sender {
NSString *post =[[NSString alloc]initWithFormat:#"keyword=%#",[keyword text]];
NSLog(#"PostData: %#",post);
......
......
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"Response ==> %#", responseData);
.....
.....
for (id obj in dic) {
NSString *nameNameObj=[obj objectForKey:#"name"];
[name addObject:nameNameObj];
NSString *userNameObj=[obj objectForKey:#"username"];
[username addObject:userNameObj];
}
.......
.......
[self.tableView reloadData];
}
}
Hope this helps... :)
There might be some of the reason below:
Make sure you set Delegate and Datasource correctly.
When you receive response reload tableView on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
Return valid cell from
cellForRowAtIndexPath:
Return valid number of cell from
numberOfRowInSection:

Related Links

UIBezierPath: byRoundingCorners: doesn't work
How To Change The Title Color of an MFMailComposeViewController?
Preventing UIImagePicker memory problems
CoreData. Manage multiple coordinators, in multi thread, multi context
AVPlayer(ViewController): can not play HLS HD video
Duplicate symbols for architecture when using global constants [duplicate]
iOS: UIWebView display link from website [duplicate]
In App Purchases returns error
NSAttributedString with image doesn't work in UITextField?
How to generate iPhone simulator build or .zip file for submit in Facebook for review in iOS
Xcode lost connection with simulator iOS 9
UIView not moving when I change it's x origin in swift
Disabling iCloud backup, tmp directory and replacement of files in path
Type cast class conforming to sub-protocol to parent-protocol in Swift
When searchController is active, status bar style changes
Draggable UITextView Not Responding

Categories

HOME
elastic-beanstalk
dynamic-programming
sharepoint-2013
pyqt5
auth0
android-activity
azure-web-sites
word-vba
typeahead.js
passwords
genetic-algorithm
weblogic11g
sony-camera-api
build.gradle
splunk
jena
eps
square-connect
stm
pie-chart
eloqua
axure
cloudrail
rgb
org-mode
gitlab-ci-runner
jscodeshift
opencms
similarity
digital-logic
pdfa
flexjs
f2py
parallax.js
solarwindslem
android-imageview
concurrenthashmap
directx-12
background-process
imagemapster
json-rpc
transpose
cookiestore
weebly
symfony-process
angular2-seed
using
xargs
zfs
rtems
ios-app-group
canopen
infiniband
attributerouting
reactivex
maven-versions-plugin
jce
liclipse
postmark
ipmi
itamae
error-correction
tfs2008
webtrends
xnamespace
agents-jade
android-number-picker
geneos
submission
oracle-policy-automation
airplay
lidar
egl
php-opencloud
asp.net-authorization
kinto
ane
multiscreen
realm-list
wss4j
mkpolyline
repaint
bunny
srp
jxta
batman.js
sqlprofiler
vlfeat
siblings
emacs-jedi
double-precision
icon-fonts
dynamic-usercontrols
12factor
xsocket
browserid
applaud
lungojs
51degrees
msxsl
arr
openid4java
emacs-ecb
wiki-markup
symstore
program-transformation
arraycopy
wmd
posterous
non-relational-database
code-statistics
nhaml
roguelike

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App