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

how to make overlay with activity indicator programatically
performseguewithidentifier works with a delay
Type [TeamModel] has no subscript members
Pdf file in webview not scrolling
Ternary Conditional Operator for nil/not nil
Xamarin / IOS / Azure - No 'id' member found on type
iOS 9 core location crash
Better accessibility label for a clickable table view cell
iOS add fixed footer UIView to multiscreen app
how to send data from iOS prototype cell to watch with swift 2.2
What does this Google Maps iOS SDK code in Swift do?
Firebase + Swift: Unable to delete rows in tableView
AVfoundation blur background in Video
How to play music in background in Swift with MPMediaPickerController chosen songs?
Assigning to ID Nullable from Incompatible Types
Swift: Utilizing CLLocationManagerDelegate and CoreLocation.framework results in “Use of undeclared type” error

Categories

HOME
validation
identityserver4
collections
orbeon
x-editable
apt-get
backand
jqgrid
zipfile
identifier
graph-tool
pyfftw
partitioning
teraterm
durandal-2.0
zap
silk-performer
snort
exe4j
smartcard
game-maker-language
iteration
buffer-overflow
debugview
dapper-fastcrud
pmd
kubernetes-go-client
react-redux-form
background-process
ollydbg
double-click-advertising
presto
msal
json-rpc
ecdsa
flags
h5py
essbase
hawkular
sections
website-hosting
xssf
infiniband
attributerouting
biological-neural-network
tryton
jvm-arguments
nuget-package-restore
iseries-navigator
masspay
cabal-install
xceed
file-management
android-number-picker
flotr2
cucumber-java
storage-duration
uac
par
post-processor
interpreted-language
hjson
biojava
twitter-follow
imaging
cpp-netlib
spread-toolkit
ituneslibrary
angularjs-google-maps
blazeds
jxta
nmea
arbtt
impdp
hamlc
hogan.js
osmf
dependency-walker
xsocket
rc-shell
media-manager
websolr
ladon
ssao
noaa
n900
wmd
pinging
coords
virtual-server

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