|
|
|
iOS - รับค่ามาเป็น NSMutableString แต่ต้องการดึงข้อมูลใส่TableView |
|
|
|
|
|
|
|
รับค่ามาเป็น NSMutableString แต่ต้องการดึงข้อมูลใส่TableView
NSMutableString *post = [NSString stringWithFormat:@"sStudentID=%@",[self.sStudentID description]];
Code (Objective-C)
//
// logindetailViewController.m
// Loginapp
//
// Created by Nakornjinda on 3/19/2557 BE.
// Copyright (c) 2557 Nakornjinda. All rights reserved.
//
#import "logindetailViewController.h"
#import "loginViewController.h"
@interface logindetailViewController ()
{
NSMutableArray*myObject;
// A Dictionary object
NSDictionary*dict;
//Define keys
NSString*StudentID;
NSString*Idname;
NSString*Namebook;
NSString*LNameT;
NSString*FNameT;
}
@end
@implementation logindetailViewController
@synthesize receivedData;
- (void)viewDidLoad
{
[super viewDidLoad];
//Define Keys
StudentID=@"StudentID";
Idname=@"Idname";
Namebook=@"Namebook";
LNameT=@"LNameT";
FNameT=@"FNameT";
[super viewDidLoad];
//sMemberID=1
NSMutableString *post = [NSString stringWithFormat:@"sStudentID=%@",[self.sStudentID description]];
// Create array to hold dictionaries
myObject = [[NSMutableArray alloc] init];
NSURLRequest *theRequest =
[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/app/getUser.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
self.receivedData = [[NSMutableData data] retain];
} else {
UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[connectFailMessage show];
[connectFailMessage release];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[receivedData release];
// inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];
[didFailWithErrorMessage release];
//inform the user
NSLog(@"Connection failed! Error - %@", [error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(receivedData)
{
//NSLog(@"%@",receivedData);
//NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
//NSLog(@"%@",dataString);
id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil];
// values in foreach loop
for (NSDictionary *dataDict in jsonObjects) {
NSString *strStudentID = [dataDict objectForKey:@"StudentID"];
NSString *strIdName = [dataDict objectForKey:@"IdName"];
NSString *strNamebook = [dataDict objectForKey:@"Namebook"];
NSString *strLNameT = [dataDict objectForKey:@"LNameT"];
NSString *strFNameT = [dataDict objectForKey:@"FNameT"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
strStudentID, StudentID,
strIdName, Idname,
strNamebook, Namebook,
strLNameT, LNameT,
strFNameT, FNameT,
nil];
[myObject addObject:dict];
}
[myTable reloadData];
}
// release the connection, and the data object
[connection release];
[receivedData release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int nbCount = [myObject count];
if (nbCount == 0)
return 1;
else
return [myObject count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int nbCount = [myObject count];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Use the default cell style.
cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle
reuseIdentifier : CellIdentifier] autorelease];
}
if (nbCount ==0)
cell.textLabel.text = @"Loading Data";
else
{
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:StudentID]];
cell.textLabel.text = [tmpDict objectForKey:Namebook];
cell.detailTextLabel.text= [tmpDict objectForKey:FNameT];
}
//[tmpDict objectForKey:galleryid]
//[tmpDict objectForKey:name]
//[tmpDict objectForKey:titlename]
//[tmpDict objectForKey:thumbnail]
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[myTable release];
[super dealloc];
}
@end
Tag : Mobile, MySQL, iOS, iPhone, Objective-C, Mac
|
|
|
|
|
|
Date :
2014-03-19 18:31:50 |
By :
fern_105 |
View :
946 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
บทความี้เลยครับ
iOS/iPhone PHP/MySQL and JSON Parsing (Objective-C)
|
|
|
|
|
Date :
2014-03-20 09:11:02 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันไม่ขึ้นอ่ะค่ะ อาจจะเป็นเพราะ มันต้องรับค่าตัวแปรมาจากหน้า login sStudentID ค่ะ ให้มันเช็คค่าจากตัว sSturdentID แล้วดึงข้อมูลตาม ID นั้น มาโชว์ใน Table ค่ะ
Code (Objective-C)
//
// logindetailViewController.m
// Loginapp
//
// Created by Nakornjinda on 3/19/2557 BE.
// Copyright (c) 2557 Nakornjinda. All rights reserved.
//
#import "logindetailViewController.h"
#import "loginViewController.h"
@interface logindetailViewController ()
{
NSMutableArray*myObject;
NSMutableArray *displayObject;
// A Dictionary object
NSDictionary*dict;
//Define keys
NSString*lblStudentID;
NSString*lblIdname;
NSString*lblNamebook;
NSString*lblLNameT;
NSString*lblFNameT;
UIAlertView *loading;
}
@end
@implementation logindetailViewController
@synthesize receivedData;
- (void)viewDidLoad
{
[super viewDidLoad];
//Define Keys
lblStudentID=@"StudentID";
lblIdname=@"Idname";
lblNamebook=@"Namebook";
lblLNameT=@"LNameT";
lblFNameT=@"FNameT";
// Create array to hold dictionaries
myObject = [[NSMutableArray alloc] init];
//Keyword=abc
NSMutableString *post = [NSString stringWithFormat:@"sStudentID =%@",[ self.sStudentID description]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSData *jsonData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"https://www.thaicreate.com/url/getJSON.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
// values in foreach loop
for (NSDictionary *dataDict in jsonObjects) {
NSString *strStudentID = [dataDict objectForKey:@"StudentID"];
NSString *strNamebook = [dataDict objectForKey:@"Namebook"];
NSString *strLNameT = [dataDict objectForKey:@"LNameT"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
strStudentID, lblStudentID,
strNamebook, lblNamebook,
strLNameT, lblLNameT,
nil];
[myObject addObject:dict];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Use the default cell style.
cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle
reuseIdentifier : CellIdentifier] autorelease];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
// MemberID
NSMutableString *text;
text = [NSString stringWithFormat:@"StudentID : %@",[tmpDict objectForKey:lblStudentID]];
// Name & Tel
NSMutableString *detail;
detail = [NSString stringWithFormat:@"Name : %@ , Tel : %@"
,[tmpDict objectForKey:lblNamebook]
,[tmpDict objectForKey:lblLNameT]];
cell.textLabel.text = text;
cell.detailTextLabel.text= detail;
//[tmpDict objectForKey:memberid]
//[tmpDict objectForKey:name]
//[tmpDict objectForKey:tel]
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[myTable release];
[super dealloc];
}
@end
|
|
|
|
|
Date :
2014-03-20 15:39:56 |
By :
fern_105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|