开源中国iOS客户端学习——(十)搜索功能

来源:互联网 发布:pdf转换软件 编辑:程序博客网 时间:2024/06/05 11:03

开源中国社区团队基于开源项目 GitLab 开发了一款和GitHub一样的在线代码托管平台 Git @ OSC。并且开源客户端的源码在GitHub上不做更新,迁移到Git @OSC上了,欲了解更多请访问Git @ OSC官网http://git.oschina.net

客户端最新源码下载地址:http://git.oschina.net/oschina/iphone-app


        回归正题,今天分析的是开源中国iOS客户端搜索功能涉及道一些知识,XML解析和动态加载表视图单元格;

在软件首页右上角有一个搜索按钮,点击进入搜索界面,当搜索的内容很多时我们下拉点击 “下面20项。。。”可以在加载20项,这些数据如何填充到表视图之中?

      


负责搜索功能的是search下的searchView类,xib控件已经已经做好布局,首先说下SearchView.h文件属性成员代表的作用;

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2. #import "SearchResult.h"  
  3. #import "MBProgressHUD.h"  
  4. @interface SearchView : UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>  
  5. {  
  6. //    可变数组存放解析的数据  
  7.     NSMutableArray * results;  
  8. //    搜索的时候判断是否正在加载数据  
  9.     BOOL isLoading;  
  10. //    判断数据是否加载完毕  
  11.     BOOL isLoadOver;  
  12. //    记录表视图单元格应该加载数据总条数  
  13.     int allCount;  
  14. }  
  15. @property (strong, nonatomic) IBOutlet UISegmentedControl *segmentSearch;  
  16. @property (strong, nonatomic) IBOutlet UITableView *tableResult;  
  17. @property (strong, nonatomic) IBOutlet UISearchBar *_searchBar;  
  18. //根据搜索关键字在不同分类中进行搜索  
  19. - (IBAction)segementChanged:(id)sender;  
  20. //搜索  
  21. -(void)doSearch;  
  22. //清空上次搜索记录  
  23. -(void)clear;  


OK现在挺进SearchView.m文件,如果搜索的内容不为空开始 dosearch方法,dosearch方法中使用了ASNetwork类库封装的post网络请求方法(关于AFNetwork post  get请求方法请看http://blog.csdn.net/duxinfeng2010/article/details/8620901)

[cpp] view plaincopy
  1. - (void)postPath:(NSString *)path  
  2.       parameters:(NSDictionary *)parameters  
  3.          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success  
  4.          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure  


post请求无法获取它的url,但是可以取出请求成功返回来的数据,比如搜索iOS  返回的xml


[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <oschina>  
  3.   <pagesize>20</pagesize>  
  4.   <results>  
  5.             <result>  
  6.       <objid>18397</objid>  
  7.       <type>software</type>  
  8.       <title><![CDATA[iOS应用开发模板 iOS Boilerplate]]></title>  
  9.       <url><![CDATA[http://www.oschina.net/p/ios-boilerplate]]></url>  
  10.       <pubDate></pubDate>  
  11.       <author></author>  
  12.     </result>  
  13.             <result>  
  14.       <objid>18977</objid>  
  15.       <type>software</type>  
  16.       <title><![CDATA[ ios-static-libraries]]></title>  
  17.       <url><![CDATA[http://www.oschina.net/p/ios-static-libraries]]></url>  
  18.       <pubDate></pubDate>  
  19.       <author></author>  
  20.     </result>  
  21.             <result>  
  22.       <objid>23309</objid>  
  23.       <type>software</type>  
  24.       <title><![CDATA[ 仿陌陌的ios客户端]]></title>  
  25.       <url><![CDATA[http://www.oschina.net/p/momo-ios-app]]></url>  
  26.       <pubDate></pubDate>  
  27.       <author></author>  
  28.     </result>  
  29.             <result>  
  30.       <objid>22121</objid>  
  31.       <type>software</type>  
  32.       <title><![CDATA[iOS任务管理器 cheddar-ios]]></title>  
  33.       <url><![CDATA[http://www.oschina.net/p/cheddar-ios]]></url>  
  34.       <pubDate></pubDate>  
  35.       <author></author>  
  36.     </result>  
  37.             <result>  
  38.       <objid>22900</objid>  
  39.       <type>software</type>  
  40.       <title><![CDATA[白宫网站 iOS 客户端 wh-app-ios]]></title>  
  41.       <url><![CDATA[http://www.oschina.net/p/wh-app-ios]]></url>  
  42.       <pubDate></pubDate>  
  43.       <author></author>  
  44.     </result>  
  45.             <result>  
  46.       <objid>17045</objid>  
  47.       <type>software</type>  
  48.       <title><![CDATA[iPhone操作系统 iOS]]></title>  
  49.       <url><![CDATA[http://www.oschina.net/p/ios]]></url>  
  50.       <pubDate></pubDate>  
  51.       <author></author>  
  52.     </result>  
  53.             <result>  
  54.       <objid>25685</objid>  
  55.       <type>software</type>  
  56.       <title><![CDATA[iOS 弹出菜单 MLPPopupMenu]]></title>  
  57.       <url><![CDATA[http://www.oschina.net/p/mlppopupmenu]]></url>  
  58.       <pubDate></pubDate>  
  59.       <author></author>  
  60.     </result>  
  61.             <result>  
  62.       <objid>22803</objid>  
  63.       <type>software</type>  
  64.       <title><![CDATA[iOS日历控件 PMCalendar]]></title>  
  65.       <url><![CDATA[http://www.oschina.net/p/pmcalendar]]></url>  
  66.       <pubDate></pubDate>  
  67.       <author></author>  
  68.     </result>  
  69.             <result>  
  70.       <objid>24390</objid>  
  71.       <type>software</type>  
  72.       <title><![CDATA[iOS 功能测试框架 calabash-ios]]></title>  
  73.       <url><![CDATA[http://www.oschina.net/p/calabash-ios]]></url>  
  74.       <pubDate></pubDate>  
  75.       <author></author>  
  76.     </result>  
  77.             <result>  
  78.       <objid>24665</objid>  
  79.       <type>software</type>  
  80.       <title><![CDATA[ iOS-Tree-Component]]></title>  
  81.       <url><![CDATA[http://www.oschina.net/p/ios-tree-component]]></url>  
  82.       <pubDate></pubDate>  
  83.       <author></author>  
  84.     </result>  
  85.             <result>  
  86.       <objid>22217</objid>  
  87.       <type>software</type>  
  88.       <title><![CDATA[ ios-calendar]]></title>  
  89.       <url><![CDATA[http://www.oschina.net/p/ios-calendar]]></url>  
  90.       <pubDate></pubDate>  
  91.       <author></author>  
  92.     </result>  
  93.             <result>  
  94.       <objid>22380</objid>  
  95.       <type>software</type>  
  96.       <title><![CDATA[ PaperFold-for-iOS]]></title>  
  97.       <url><![CDATA[http://www.oschina.net/p/paperfold-for-ios]]></url>  
  98.       <pubDate></pubDate>  
  99.       <author></author>  
  100.     </result>  
  101.             <result>  
  102.       <objid>21763</objid>  
  103.       <type>software</type>  
  104.       <title><![CDATA[ drupal-ios-sdk]]></title>  
  105.       <url><![CDATA[http://www.oschina.net/p/drupal-ios-sdk]]></url>  
  106.       <pubDate></pubDate>  
  107.       <author></author>  
  108.     </result>  
  109.             <result>  
  110.       <objid>19628</objid>  
  111.       <type>software</type>  
  112.       <title><![CDATA[iOS开发基础工具包 BaseAppKit]]></title>  
  113.       <url><![CDATA[http://www.oschina.net/p/baseappkit]]></url>  
  114.       <pubDate></pubDate>  
  115.       <author></author>  
  116.     </result>  
  117.             <result>  
  118.       <objid>20637</objid>  
  119.       <type>software</type>  
  120.       <title><![CDATA[iOS消息提醒库 TBHintView]]></title>  
  121.       <url><![CDATA[http://www.oschina.net/p/tbhintview]]></url>  
  122.       <pubDate></pubDate>  
  123.       <author></author>  
  124.     </result>  
  125.             <result>  
  126.       <objid>21246</objid>  
  127.       <type>software</type>  
  128.       <title><![CDATA[iOS 弹出式菜单 MGTileMenu]]></title>  
  129.       <url><![CDATA[http://www.oschina.net/p/mgtilemenu]]></url>  
  130.       <pubDate></pubDate>  
  131.       <author></author>  
  132.     </result>  
  133.             <result>  
  134.       <objid>23498</objid>  
  135.       <type>software</type>  
  136.       <title><![CDATA[iOS 的 Canvas 和 Audio 实现 Ejecta]]></title>  
  137.       <url><![CDATA[http://www.oschina.net/p/ejecta]]></url>  
  138.       <pubDate></pubDate>  
  139.       <author></author>  
  140.     </result>  
  141.             <result>  
  142.       <objid>23968</objid>  
  143.       <type>software</type>  
  144.       <title><![CDATA[样式化 iOS 应用 NUI]]></title>  
  145.       <url><![CDATA[http://www.oschina.net/p/nui]]></url>  
  146.       <pubDate></pubDate>  
  147.       <author></author>  
  148.     </result>  
  149.             <result>  
  150.       <objid>20730</objid>  
  151.       <type>software</type>  
  152.       <title><![CDATA[iOS/Android 矢量图形框架 TouchVG]]></title>  
  153.       <url><![CDATA[http://www.oschina.net/p/touchvg]]></url>  
  154.       <pubDate></pubDate>  
  155.       <author></author>  
  156.     </result>  
  157.             <result>  
  158.       <objid>22356</objid>  
  159.       <type>software</type>  
  160.       <title><![CDATA[iOS日历控件 MACalendarUI]]></title>  
  161.       <url><![CDATA[http://www.oschina.net/p/macalendarui]]></url>  
  162.       <pubDate></pubDate>  
  163.       <author></author>  
  164.     </result>  
  165.           </results>  
  166. <notice>  
  167.     <atmeCount>0</atmeCount>  
  168.     <msgCount>0</msgCount>  
  169.     <reviewCount>0</reviewCount>  
  170.     <newFansCount>0</newFansCount>  
  171. </notice>  
  172. </oschina>  
  173. <!-- Generated by OsChina.NET (init:0[ms],page:13[ms],ip:61.163.231.198) -->  

要解析xm里数据必须熟悉xml文件各个节点之间关系 

根节点oschina,它的子节点pagesize返回本次加载了几条数据,子节点results,results的子节点下20条result节点,我们主要获取result内容。然后就是最后面的子节点notice节点,存放用户的一些信息如动弹情况、收到消息、回复、粉丝;在post请求中用到了一个异常处理语句 @try @catch @finally

@try 

//执行的代码,其中可能有异常。一旦发现异常,则立即跳到catch执行。否则不会执行catch里面的内容 

@catch 

//除非try里面执行代码发生了异常,否则这里的代码不会执行 

@finally 

//不管什么情况都会执行,包括try catch 里面用了return ,可以理解为只要执行了try或者catch,就一定会执行 finally 
}


给dosearch添加了一些注释

[cpp] view plaincopy
  1. -(void)doSearch  
  2. {  
  3. //    标记,表示正在加载数据中  
  4.     isLoading = YES;  
  5.     NSString * catalog;  
  6. //    switch语句中根据Segment按钮集合中按钮索引,判断搜索哪一类内容,为下面的搜索API传参  
  7.     switch (self.segmentSearch.selectedSegmentIndex) {  
  8.         case 0:  
  9.             catalog = @"software";  
  10.             break;  
  11.         case 1:  
  12.             catalog = @"post";  
  13.             break;  
  14.         case 2:  
  15.             catalog = @"blog";  
  16.             break;  
  17.         case 3:  
  18.             catalog = @"news";  
  19.             break;  
  20.     }  
  21. //使用AFNetWork使用post方式从网络请求数据  
  22.     [[AFOSCClient sharedClient] postPath:api_search_list parameters:[NSDictionary dictionaryWithObjectsAndKeys:_searchBar.text,@"content",catalog,@"catalog",[NSString stringWithFormat:@"%d", allCount/20],@"pageIndex",@"20",@"pageSize", nil] success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  23. //        取消searchBar的第一响应对象,键盘消失  
  24.         [self._searchBar resignFirstResponder];  
  25. //        在没有内容之前tableView是没有任何内容的,所以隐藏掉  
  26.         self.tableResult.hidden = NO;  
  27. //      根据请求回来的数据判断当前用户释放登陆,需要获取用户一些信息  
  28.         [Tool getOSCNotice2:operation.responseString];  
  29. //        上面属于请求数据是不回加载到视图控制器上,所以标记属性为NO  
  30.         isLoading = NO;  
  31. //        再次从xml文件中请求数据,获取当前加载数据条数,数量  
  32.         int count = [Tool isListOver2:operation.responseString];  
  33.         allCount += count;  
  34. //        将请求的xml内容给NSString对象  
  35.         NSString *response = operation.responseString;  
  36.                                                                         
  37.         NSLog(@"response =%@",response);  
  38.         @try {  
  39. //            开始解析需要显示到表视图单元格对象中的数据  
  40.             TBXML *xml = [[TBXML alloc] initWithXMLString:response error:nil];  
  41.             TBXMLElement *root = xml.rootXMLElement;  
  42. //            从次根节点获取根节点下内容  
  43.             TBXMLElement *_results = [TBXML childElementNamed:@"results" parentElement:root];  
  44.             if (!_results) {  
  45.                 isLoadOver = YES;  
  46.                 [self.tableResult reloadData];  
  47.                 return;  
  48.             }  
  49. //            获取result节点下内容  
  50.             TBXMLElement *first = [TBXML childElementNamed:@"result" parentElement:_results];  
  51.             if (!first) {  
  52.                 isLoadOver = YES;  
  53.                 [self.tableResult reloadData];  
  54.                 return;  
  55.             }  
  56. //            取出result节点下的节点  
  57.             NSMutableArray * newResults = [[NSMutableArray alloc] initWithCapacity:20];  
  58.             TBXMLElement *objid = [TBXML childElementNamed:@"objid" parentElement:first];  
  59.             TBXMLElement *type = [TBXML childElementNamed:@"type" parentElement:first];  
  60.             TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:first];  
  61.             TBXMLElement *url = [TBXML childElementNamed:@"url" parentElement:first];  
  62.             TBXMLElement *pubDate = [TBXML childElementNamed:@"pubDate" parentElement:first];  
  63.             NSString * pubDateStr = [TBXML textForElement:pubDate];  
  64.             TBXMLElement *author = [TBXML childElementNamed:@"author" parentElement:first];  
  65. //            取出节点中的值,赋给一个SearchResult对象属性  
  66.             SearchResult * s = [[SearchResult alloc] initWithParameters:[[TBXML textForElement:objid] intValue] andType:[[TBXML textForElement:type] intValue] andTitle:[TBXML textForElement:title] andUrl:[TBXML textForElement:url] andPubDate:[pubDateStr isEqualToString:@""] ? @"" : [Tool intervalSinceNow:pubDateStr] andAuthor:[TBXML textForElement:author]];  
  67. //            将获取对象添加到可变数组  
  68.             if (![Tool isRepeatSearch:results andResult:s]) {  
  69.                 [newResults addObject:s];  
  70.             }  
  71. //           在循环之中 寻找下一个节点  直至找完  
  72.             while (first) {  
  73.                 first = [TBXML nextSiblingNamed:@"result" searchFromElement:first];  
  74.                 if (first) {  
  75.                     objid = [TBXML childElementNamed:@"objid" parentElement:first];  
  76.                     type = [TBXML childElementNamed:@"type" parentElement:first];  
  77.                     title = [TBXML childElementNamed:@"title" parentElement:first];  
  78.                     url = [TBXML childElementNamed:@"url" parentElement:first];  
  79.                     pubDate = [TBXML childElementNamed:@"pubDate" parentElement:first];  
  80.                     author = [TBXML childElementNamed:@"author" parentElement:first];  
  81.                                                                         
  82.                     s = [[SearchResult alloc] initWithParameters:[[TBXML textForElement:objid] intValue] andType:[[TBXML textForElement:type] intValue] andTitle:[TBXML textForElement:title] andUrl:[TBXML textForElement:url] andPubDate:[Tool intervalSinceNow:[TBXML textForElement:pubDate]] andAuthor:[TBXML textForElement:author]];  
  83. //                    
  84.                     if (![Tool isRepeatSearch:results andResult:s]) {  
  85.                         [newResults addObject:s];  
  86.                     }  
  87.                 }  
  88. //                first = NULL  直接跳出  
  89.                 else  
  90.                 {  
  91.                     break;  
  92.                 }  
  93.             }  
  94. //            如果搜索结果数据小雨20条,表示一个页面就可以加载完毕  
  95.             if (newResults.count < 20) {  
  96.                 isLoadOver = YES;  
  97.             }  
  98. //            将解析数据添加道results之中  
  99.             [results addObjectsFromArray:newResults];  
  100. //            刷新表示图内容  
  101.             [self.tableResult reloadData];  
  102.                                                                         
  103.         }  
  104.         @catch (NSException *exception) {  
  105.             [NdUncaughtExceptionHandler TakeException:exception];  
  106.         }  
  107.         @finally {  
  108.                                                                         
  109.         }  
  110. //        请求失败  
  111.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  112.         [Tool ToastNotification:@"网络连接故障" andView:self.view andLoading:NO andIsBottom:NO];  
  113.     }];  
  114. //    刷新表视图单元内容  
  115.     [self.tableResult reloadData];  
  116. }  



 在   [Tool    getOSCNotice2:operation.responseString];解析的登陆用户一些信息


//   Tool类中

[cpp] view plaincopy
  1. + (OSCNotice *)getOSCNotice2:(NSString *)response  
  2. {  
  3.     TBXML *xml = [[TBXML alloc] initWithXMLString:response error:nil];  
  4.     TBXMLElement *root = xml.rootXMLElement;  
  5.     if (!root) {  
  6.         return nil;  
  7.     }  
  8.     TBXMLElement *notice = [TBXML childElementNamed:@"notice" parentElement:root];  
  9.     if (!notice) {  
  10.         [Config Instance].isLogin = NO;  
  11.         [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:@"0"];  
  12.         return nil;  
  13.     }  
  14.     else  
  15.     {  
  16.         [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:@"1"];  
  17.         [Config Instance].isLogin = YES;  
  18.     }  
  19.     TBXMLElement *atme = [TBXML childElementNamed:@"atmeCount" parentElement:notice];  
  20.     TBXMLElement *msg = [TBXML childElementNamed:@"msgCount" parentElement:notice];  
  21.        TBXMLElement *review = [TBXML childElementNamed:@"reviewCount" parentElement:notice];  
  22.     TBXMLElement *newFans = [TBXML childElementNamed:@"newFansCount" parentElement:notice];  
  23.     OSCNotice *oc = [[OSCNotice alloc] initWithParameters:[[TBXML textForElement:atme] intValue] andMsg:[[TBXML textForElement:msg] intValue] andReview:[[TBXML textForElement:review] intValue] andFans:[[TBXML textForElement:newFans] intValue]];  
  24.     [[NSNotificationCenter defaultCenter] postNotificationName:Notification_NoticeUpdate object:oc];  
  25.                                                                                                                                                                                                                                                                   
  26.     return oc;  
  27. }  


        [Tool   isListOver2:operation.responseString];  用于也是解析数据,获取返回的数据条数,告诉table将要显示多少行cell,当把cell加载到最后的时候获取下面20项,或跟多,然后把这些数据存放道allcount里面,所以就有allCount += count


            Tool类中,解析返回一个数据 pagesize,显示多少行


[cpp] view plaincopy
  1. + (int)isListOver2:(NSString *)response  
  2. {  
  3.     TBXML *xml = [[TBXML alloc] initWithXMLString:response error:nil];  
  4.     TBXMLElement *root = xml.rootXMLElement;  
  5.     TBXMLElement *pageSize = [TBXML childElementNamed:@"pagesize" parentElement:root];  
  6.     int size = [[TBXML textForElement:pageSize] intValue];  
  7.     return size;  
  8. }  


然后进入到try中又一次解析获取result里面数据,这里有请求了一次数据,有解析了一边,感觉这里处理的不是很好,同一个返回数据请求了三次,如果用户用的不是wifi就可能耗费流量浪费电量;


剩下的就是表示图加载数据了


[cpp] view plaincopy
  1. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  2. {  
  3. //    如何加载完成,返回数据为空,返回1,这个单元格是显示一个提示,“查无结果”  如果返回不为空,返回results.count + 1  个显示结果,最后加 1 ,是显示加载数据超过20条的时候 点击 “下面20项”时加载更多数据  
  4.     if (isLoadOver) {  
  5.         return results.count == 0 ? 1 : results.count;  
  6.     }  
  7.     else  
  8.         return results.count + 1;  
  9. }  
  10. //处理cell的行高  
  11. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  12. {  
  13.     if (isLoadOver) {  
  14.         return results.count == 0 ? 62 : 50;  
  15.     }  
  16.     else  
  17.     {  
  18.         return indexPath.row < results.count ? 50 : 62;  
  19.     }  
  20. }  
  21.   
  22. //处理tableView背景色  
  23. -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
  24. {  
  25.     cell.backgroundColor = [Tool getCellBackgroundColor];  
  26. }  
  27.   
  28. //定制单元格的显示内容  
  29. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  30. {  
  31.     if (results.count > 0)   
  32.     {  
  33.         if (indexPath.row < results.count)   
  34.         {  
  35.             UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];  
  36.             if (!cell) {  
  37.                 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NormalCellIdentifier];  
  38.             }  
  39.             SearchResult * s = [results objectAtIndex:indexPath.row];  
  40.             cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];  
  41.             cell.textLabel.text = s.title;  
  42.             if (self.segmentSearch.selectedSegmentIndex != 0)   
  43.             {  
  44.                 cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ 发表于 %@", s.author, s.pubDate];  
  45.             }  
  46.             else  
  47.             {  
  48.                 cell.detailTextLabel.text = @"";  
  49.             }  
  50.             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  51.             return cell;  
  52.         }  
  53.         else  
  54.         {  
  55.             return [[DataSingleton Instance] getLoadMoreCell:tableView andIsLoadOver:isLoadOver andLoadOverString:@"搜索完毕" andLoadingString:(isLoading ? loadingTip : loadNext20Tip) andIsLoading:isLoading];  
  56.         }  
  57.     }  
  58. //    如果搜索返回的数据为空  提示  查无结果  
  59.     else  
  60.     {  
  61.         return [[DataSingleton Instance] getLoadMoreCell:tableView andIsLoadOver:isLoadOver andLoadOverString:@"查无结果" andLoadingString:(isLoading ? loadingTip : loadNext20Tip) andIsLoading:isLoading];  
  62.     }  
  63. }  
  64.   
  65. //选中某一行的时候显示该条信息的详细内容  
  66. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  67. {  
  68.     [self._searchBar resignFirstResponder];  
  69.     [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  70.     int row = indexPath.row;  
  71.     if (row >= results.count)   
  72.     {  
  73.         if (!isLoading && !isLoadOver)   
  74.         {  
  75.             [self performSelector:@selector(doSearch)];  
  76.         }  
  77.     }  
  78.     else  
  79.     {  
  80.         SearchResult * s = [results objectAtIndex:row];  
  81.         if (s)   
  82.         {  
  83.             [Tool analysis:s.url andNavController:self.navigationController];  
  84.              
  85.         }  
  86.     }  
  87. }  


打开某一条信息,并查看其详细信息调用 analysis: andNavController:,该方法里针对传入URL,如果是站外连接 比如某个软件官网,直接跳转到该软件的官网上,如果是开源中国社区站内连接,就可能需要加载一些这条信息的评论详情如果检测道用户登陆给予用户品论权限和分享功能;具体实现如下

[cpp] view plaincopy
  1. + (BOOL)analysis:(NSString *)url andNavController:(UINavigationController *)navController  
  2. {  
  3.     NSString *search = @"oschina.net";  
  4.     //判断是否包含 oschina.net 来确定是不是站内链接  
  5.     NSRange rng = [url rangeOfString:search];  
  6.     if (rng.length <= 0) {  
  7.         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  
  8.         return NO;  
  9.     }  
  10.     //站内链接  
  11.     else  
  12.     {  
  13.         url = [url substringFromIndex:7];  
  14.         NSString *prefix = [url substringToIndex:3];  
  15.         //此情况为 博客,动弹,个人专页  
  16.         if ([prefix isEqualToString:@"my."])   
  17.         {  
  18.             NSArray *array = [url componentsSeparatedByString:@"/"];  
  19.             //个人专页 用户名形式  
  20.             if ([array count] <= 2) {  
  21.                 [Tool pushUserDetailWithName:[array objectAtIndex:1] andNavController:navController];  
  22.                 return YES;  
  23.             }  
  24.             //个人专页 uid形式  
  25.             else if([array count] <= 3)  
  26.             {  
  27.                 if ([[array objectAtIndex:1] isEqualToString:@"u"]) {  
  28.                     [Tool pushUserDetail:[[array objectAtIndex:2] intValue] andNavController:navController];  
  29.                     return YES;  
  30.                 }  
  31.             }  
  32.             else if([array count] <= 4)  
  33.             {  
  34.                 NSString *type = [array objectAtIndex:2];  
  35.                 if ([type isEqualToString:@"blog"]) {  
  36.                     News *n = [[News alloc] init];  
  37.                     n.newsType = 3;  
  38.                     n.attachment = [array objectAtIndex:3];  
  39.                     [Tool pushNewsDetail:n andNavController:navController andIsNextPage:NO];  
  40.                     return YES;  
  41.                 }  
  42.                 else if([type isEqualToString:@"tweet"]){  
  43.                     Tweet *t = [[Tweet alloc] init];  
  44.                     t._id = [[array objectAtIndex:3] intValue];  
  45.                     [Tool pushTweetDetail:t andNavController:navController];  
  46.                     return YES;  
  47.                 }  
  48.             }  
  49.             else if(array.count <= 5)  
  50.             {  
  51.                 NSString *type = [array objectAtIndex:3];  
  52.                 if ([type isEqualToString:@"blog"]) {  
  53.                     News *n = [[News alloc] init];  
  54.                     n.newsType = 3;  
  55.                     n.attachment = [array objectAtIndex:4];  
  56.                     [Tool pushNewsDetail:n andNavController:navController andIsNextPage:NO];  
  57.                     return YES;  
  58.                 }  
  59.             }  
  60.         }  
  61.         //此情况为 新闻,软件,问答  
  62.         else if([prefix isEqualToString:@"www"])  
  63.         {  
  64.             NSArray *array = [url componentsSeparatedByString:@"/"];  
  65.             int count = [array count];  
  66.             if (count>=3) {  
  67.                 NSString *type = [array objectAtIndex:1];  
  68.                 if ([type isEqualToString:@"news"]) {  
  69.   
  70.                     int newsid = [[array objectAtIndex:2] intValue];  
  71.                     News *n = [[News alloc] init];  
  72.                     n.newsType = 0;  
  73.                     n._id = newsid;  
  74.                     [Tool pushNewsDetail:n andNavController:navController andIsNextPage:YES];  
  75.                     return YES;  
  76.                 }  
  77.                 else if([type isEqualToString:@"p"]){  
  78.                     News *n = [[News alloc] init];  
  79.                     n.newsType = 1;  
  80.                     n.attachment = [array objectAtIndex:2];  
  81.                     [Tool pushNewsDetail:n andNavController:navController andIsNextPage:NO];  
  82.                     return YES;  
  83.                 }  
  84.                 else if([type isEqualToString:@"question"]){  
  85.                     if (count == 3) {  
  86.                         NSArray *array2 = [[array objectAtIndex:2] componentsSeparatedByString:@"_"];  
  87.                         if ([array2 count] >= 2) {  
  88.                             int _id = [[array2 objectAtIndex:1] intValue];  
  89.                             Post *p = [[Post alloc] init];  
  90.                             p._id = _id;  
  91.                             [Tool pushPostDetail:p andNavController:navController];  
  92.                             return YES;  
  93.                         }  
  94.                     }  
  95.                     else if(count >= 4)  
  96.                     {  
  97. //                        NSString *tag = [array objectAtIndex:3];  
  98.                         NSString *tag = @"";  
  99.                         if (array.count == 4) {  
  100.                             tag = [array objectAtIndex:3];  
  101.                         }  
  102.                         else  
  103.                         {  
  104.                             for (int i=3; i<count-1; i++) {  
  105.                                 tag = [NSString stringWithFormat:@"%@/%@", [array objectAtIndex:i],[array objectAtIndex:i+1]];  
  106.                             }  
  107.                         }  
  108.                         NSString *tag2 = [tag stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
  109.                         PostsView *pview = [PostsView new];  
  110.                         pview.tag = tag;  
  111.                         pview.navigationItem.title = [NSString stringWithFormat:@"%@", tag2];  
  112.                         [navController pushViewController:pview animated:YES];  
  113.                         return  YES;  
  114.                     }  
  115.                 }  
  116.             }  
  117.         }  
  118. //        根据一个URL打开一个网页  
  119.         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", url]]];  
  120.         return NO;  
  121.    }  
  122. }  



原创博客欢迎转载分享,请注明出处http://blog.csdn.net/duxinfeng2010