Copyright © Mike's Blog
Design by Dzignine
2011年8月8日 星期一

一些iphone的小東西

最近在開發iphone一個case
遇到很多小問題 也學到很多實用技巧
在這邊記錄一下 也許以後有幫助

/**
 * 格式化日期
 */
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]];

//顯示結果
//2011-08-08


/**
 * 儲存plist檔案
 */
NSMutableDictionary *log = [[NSMutableDictionary alloc] init];
[log setValue:@"hello world" forKey:@"test"];
[log writeToFile:logPath atomically:YES];


/**
 * 使用get抓檔案
 */
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"你的網址"]];
[NSURLConnection connectionWithRequest:request delegate:self];

//收到網站的回應
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response
//收到網站傳回的資料
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
//網路連線錯誤
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
//網頁資料下載完畢
- (void)connectionDidFinishLoading:(NSURLConnection *)connection

//不過特別注意的是有網路可是伺服器的問題
//網頁一樣資料會被抓下來 不過出現網路錯誤
//可以用response的status code去判斷
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {  
    int statusCode = [(NSHTTPURLResponse *) response statusCode];
    if ( statusCode < 200 && statusCode >207) {
        //http回應不正確
    }
} 


/**
 * UITableView的cell 間隔背景色
 */
UITableView *result = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
result.dataSource = self;
result.delegate = self;

//UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row % 2) {
        cell.backgroundColor = [UIColor whiteColor];
    }
    else {
        
        cell.backgroundColor = [UIColor colorWithRed:236.0/255.0 green:255.0/255.0 blue:201.0/255.0 alpha:1.0];
        
    }
}

0 意見:

張貼留言