iOS - อยากสอบถามเก่ียวกับการทำให้รูปเปลี่ยนไปตามค่าในฐานข้อมูลค่ะ จะต้องทำยังงัย ตอนนี้กำลังทำหน้า ค้นหาหนังสืออย่ค่ะแต่ต้องโชว์สถานะการยืม
ตอนนี้พวก ดาว ใช้การ Query หรือแสดง แบบไหนครับ
Date :
2014-02-10 06:29:44
By :
mr.win
ยังไม่ได้ query สถานะเลยค่ะถ้าต้องการquery ต้อง ทำงัยค่ะ อันนี้แค่โชว์รูปเฉยๆค่ะ
Date :
2014-02-10 10:41:49
By :
fern_105
Code (Objective-C)
//
// searchView.m
// searchapp
//
// Created by Nakornjinda on 2/8/2557 BE.
// Copyright (c) 2557 Nakornjinda. All rights reserved.
//
#import "searchView.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;
UIAlertView *loading;
}
@end
@implementation searchView
@synthesize receivedData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Define keys
idbook = @"Idbook";
title1 = @"title1";
access = @"access";
w_name = @"w_name";
status = @"status1";
// Create array to hold dictionaries
myObject = [[NSMutableArray alloc] init];
}
- (IBAction)btnSearch:(id)sender {
//Keyword=abc
NSMutableString *post = [NSString stringWithFormat:@"keyword=%@",[txtKeyword text]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/search.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Show Progress Loading...
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
loading = [[UIAlertView alloc] initWithTitle:@"" message:@"Please Wait..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loading addSubview:progress];
[progress startAnimating];
[progress release];
[loading show];
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];
}
[myTable reloadData];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
sleep(2);
[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
{
// Hide Progress
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[loading dismissWithClickedButtonIndex:0 animated:YES];
// Clear Object
[myObject removeAllObjects];
if(receivedData)
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData 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"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
strIdbook, idbook,
strtitle1, title1,
straccess, access,
strw_name, w_name,
strstatus1,status,
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
{
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];
}
int nbCount = [myObject count];
if (nbCount ==0)
cell.textLabel.text = @"";
else
{
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
//
NSMutableString *text;
text = [NSString stringWithFormat:@"ชื่อหนังสือ %@",[tmpDict objectForKey:title1]];
//
NSMutableString *detail;
detail = [NSString stringWithFormat:@"รหัส%@ , ผู้แต่ง %@, %@"
,[tmpDict objectForKey:access]
,[tmpDict objectForKey:w_name]
,[tmpDict objectForKey:status]];
cell.imageView.image=[UIImage imageNamed : @"1StarSmall.png"];
cell.textLabel.text = text;
cell.detailTextLabel.text= detail;
}
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)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
[txtKeyword resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=@"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[txtKeyword release];
[myTable release];
[super dealloc];
}
- (IBAction)BackToViewController:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
} //โค้ดติดต่ดปุ่มกลับหน้า
//- (IBAction)gotoOneViewController:(id)sender {
// OneViewController *view1 = [[OneViewController alloc] initWithNibName:nil bundle:nil];
//[self presentViewController:view1 animated:NO completion:NULL];
//} //โค้ดไปหน้า OneViewController *view1
@end
Date :
2014-02-11 23:09:46
By :
fern_105
Load balance : Server 05