小秋的家

          home

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks
          1. 如何除去UITableViewStyleGroup類型的UITableView的邊框:

              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,其他的不通過就行了!!!


          posted on 2011-08-11 13:48 棋劍小秋 閱讀(2512) 評論(0)  編輯  收藏 所屬分類: iPhone
          主站蜘蛛池模板: 襄垣县| 巴彦县| 南木林县| 新余市| 漠河县| 安化县| 辉南县| 九寨沟县| 和林格尔县| 铁力市| 资兴市| 剑川县| 莎车县| 北川| 西和县| 兴海县| 洛扎县| 隆德县| 大城县| 山东| 武威市| 潼关县| 巴中市| 措勤县| 兖州市| 郸城县| 黑河市| 威信县| 海伦市| 博罗县| 磴口县| 徐闻县| 隆林| 柳州市| 克拉玛依市| 枣阳市| 日喀则市| 邢台市| 土默特右旗| 宜都市| 灵台县|