UITableViewStylePlain類型的UITableView去除邊框線有直接的屬性方法:
separatorStyle = UITableViewCellSeparatorStyleNone;
但在UITableViewStyleGrouped類型的UITableView中,該方法無效如何去除邊框線呢?答案很簡單:
separatorColor=[UIColor clearColor];
2. UITableView劃動刪除的實現
需要實現下面兩個方法
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataArray removeObjectAtIndex:indexPath.row];
// Delete the row from the data source. [testTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
3. NSURLConnection同步,異步與SSL
同步請求
NSURL *url=[[NSURL alloc]initWithString:urlString];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
NSError *err=nil;
NSData *data=[NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:&err];
if(data==nil)
{
//if([err code])
NSLog(@"Code:%d,domain:%@,localizedDesc:%@",[err code],
[err domain],[err localizedDescription]);
}
else
{
}
這種情況,通過一個靜態方法,請求request,這種情況下,會一直阻塞,等到返回結果,簡單易用
異步請求
NSURL *url=[[NSURL alloc]initWithString:urlString];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[url release];
[request release];
if(connection)
{
receivedData = [[NSMutableData data] retain];
NSLog(@"intial done!");
}
else
{
NSLog(@"sorry");
}
通過一個delegate來做數據的下載以及Request的接受等等消息,此處delegate:self,所以需要本類實現一些方法,并且定義receivedData做數據的接受
基本上要實現下面節歌方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"get the whole response");
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"get some data");
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
基本上這樣就搞定了!!!
但是異步模式下帶來了一個新的問題,很多情況下,網絡請求不在主線程,或者界面等待網絡結果,不在主線程的時候,調用線程如果生命周期over,下面這些可能都沒有調用到,導致得不到想要得效果,所以需要在NSURLConnection請求后面加點東西來阻塞
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
好了,現在我們看看SSL的問題,在NSURLConnnection本來有方法可以跳過ssl檢查,可惜被apple無情的私有了,所以同步的數據請求肯定不行了,看看文檔,只能通過異步delegate的方式了
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
第一個方法會根據你的URL來判斷是否需要做認證
第二個方法是認證的過程,if ([trustedHosts containsObject:challenge.protectionSpace.host]),這行代碼注釋掉,就可以自動所有SSL通過,否則,你可以加一些Trust的hosts,其他的不通過就行了!!!