NSDictionary的遍歷
//方法一
NSEnumerator *enum = [myDict keyEnumerator];
id key;
while ( key = [enum nextObject] ) {
NSLog("%@ : %@", key, [myDict objectForKey:key]);
}
//方法二
for (NSString *key in myDict) {
NSLog("%@ : %@", key, [myDict objectForKey:key]);
}
簡單的網絡請求
// GET
NSString *urlStr = [NSString stringWithFormat:@"%@/food/?keyword=%@", baseURL, searchText];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
// POST
NSString *urlStr = [NSString stringWithFormat:@"%@/user/login", baseURL];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *dataStr = [NSString stringWithFormat:@"user_tel=%@&user_pass=%@", phone, password];
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:5];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
別忘了實現網絡請求代理方法:
#pragma mark - NSURLConnectionData Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
resultData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[resultData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"請求錯誤:%@", error);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"請求失敗"
message:@"網絡堵車,請檢查網絡!"
delegate:self
cancelButtonTitle:@"好"
otherButtonTitles:nil, nil];
[alertView show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (resultData.length > 0) {
// type your code here ...
}
}
如何在用戶滾動UITableView時收起鍵盤
// UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[mySearchController.searchBar resignFirstResponder];
}
提示:UITableView繼承自UIScrollView