|
|
|
iOS - ทำUIAlertView แต่มันโชว์ข้อมูลเป็นJson Array ค่ะ ไม่ทราบเป็นเพราะอะไรแล้วจะแปลง json เป็นข้อความยังงัยค่ะ |
|
|
|
|
|
|
|
ทำUIAlertView แต่มันโชว์ข้อมูลเป็นJson Array ค่ะ ไม่ทราบเป็นเพราะอะไรแล้วจะแปลง json เป็นข้อความยังงัยค่ะ
Code (Objective-C)
//
// SearchView.m
// Liberry
//
// Created by Nakornjinda on 1/15/2557 BE.
// Copyright (c) 2557 Nakornjinda. All rights reserved.
//
#import "SearchView.h"
#import "Loginoneview.h"
@interface SearchView ()
{
NSMutableArray *myObject;
NSMutableArray *allObject;
NSMutableArray *displayObject;
// A dictionary object
NSDictionary *dict;
// Define keys
NSString *idbook;
NSString *title1;
NSString *access;
NSString *w_name;
NSString *status;
NSString *bcontent;
UIAlertView *loading;
}
@end
@implementation SearchView
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Define keys
idbook = @"Idbook";
title1 = @"title1";
access = @"access";
w_name = @"w_name";
status= @"status1";
bcontent=@"content";
// Create array to hold dictionaries
allObject = [[NSMutableArray alloc] init];
NSData *jsonData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://localhost:8888/searchtest.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
// values in foreach loop
for (NSDictionary *dataDict in jsonObjects) {
NSString *strIdbook = [dataDict objectForKey:@"Idbook"];
NSString *strtitle1 = [dataDict objectForKey:@"title1"];
NSString *straccess = [dataDict objectForKey:@"access"];
NSString *strw_name= [dataDict objectForKey:@"w_name"];
NSString *strstatus1= [dataDict objectForKey:@"status1"];
NSString *strcontent= [dataDict objectForKey:@"content"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
strIdbook, idbook,
strtitle1, title1,
straccess, access,
strw_name, w_name,
strstatus1,status,
strcontent,bcontent,
nil];
[allObject addObject:dict];
}
displayObject =[[NSMutableArray alloc] initWithArray:allObject];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return displayObject.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];
}
NSDictionary *tmpDict = [displayObject objectAtIndex:indexPath.row];
NSString *cellValue;
cellValue = [tmpDict objectForKey:title1];
NSString* detailbook;
detailbook = [NSString stringWithFormat:@"รหัส%@ , ผู้แต่ง %@, สถานะ %@ "
,[tmpDict objectForKey:access]
,[tmpDict objectForKey:w_name]
,[tmpDict objectForKey:status]];
cell.textLabel.text = cellValue;
cell.detailTextLabel.text= detailbook;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if([searchString length] == 0)
{
[displayObject removeAllObjects];
[displayObject addObjectsFromArray:allObject];
}
else
{
[displayObject removeAllObjects];
for(NSDictionary *tmpDict in allObject)
{
NSString *val = [tmpDict objectForKey:title1];
NSRange r = [val rangeOfString:searchString options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
[displayObject addObject:tmpDict];
}
}
}
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=@"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Perform segue to candy detail
NSString * UserSelected;
{
NSDictionary *tmpDict = [displayObject objectAtIndex:indexPath.row];
UserSelected =[displayObject objectAtIndex:indexPath.row];
NSString * book;
book = [tmpDict objectForKey:title1];
}
NSString*str = [[NSString alloc]initWithFormat:@"รายละเอียดหนังสือ: %@",UserSelected];
UIAlertView *alv = [[UIAlertView alloc]
initWithTitle:@"รายการที่เลือก"
message:str
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alv show];
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (IBAction)BackToViewController:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
} //โค้ดติดต่ดปุ่มกลับหน้า
- (IBAction)gotoLoginoneview:(id)sender {
Loginoneview *view2 = [[Loginoneview alloc] initWithNibName:nil bundle:nil];
[self presentViewController:view2 animated:NO completion:NULL];
} //โค้ดไปหน้า OneViewController *view1
@end
Tag : PHP, MySQL, iOS, iPhone, Objective-C, Mac
|
|
|
|
|
|
Date :
2014-03-14 14:51:07 |
By :
fern_105 |
View :
929 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แปลงค่า JSON ก่อนครับ
iOS/iPhone and JSON (Create JSON and JSON Parsing, Objective-C)
|
|
|
|
|
Date :
2014-03-14 16:55:55 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ต้องใส่ตรงบันทัดไหนค่ะ ลองใส่แล้วมัน error ค่ะ
|
|
|
|
|
Date :
2014-03-15 08:32:57 |
By :
fern_105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|