2010-12-06 90 views
2

我需要以下代碼的幫助,我使用hpple來解析html。 我需要幫助利用這些數據。從數組填充一個UITableView

-(void) whatever{ 
NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding]; 
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData]; 
NSArray *titles = [xpathParser search:@"//h3"]; // get the page title - this is xpath  notation 
TFHppleElement *title = [titles objectAtIndex:0]; 
NSString *myTitles = [title content]; 

NSArray *articles = [xpathParser search:@"//h4"]; // get the page article - this is xpath  notation 
TFHppleElement *article = [articles objectAtIndex:0]; 
NSString *myArtical = [article content]; 

我想創建,並從數組填充表「標題」 然後可以點擊桌子上的物品裝入一個子視圖應該顯示相同指數在相應ARTICAL?

我想這樣做編程或使用IB

任何人都可以提出一些示例代碼或教程?

謝謝。

回答

4
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

return [titles count]; 

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 



     static NSString *MyIdentifier = @"MyIdentifier"; 

     UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
     if (cell == nil){ 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
     } 


    cell.textLabel.text = [titles objectAtIndex:indexPath.row]; 

     return cell; 

} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

/* here pop up Your subview with corresponding value from the array with array index indexpath.row ..*/ 

} 
+0

這是偉大的rajesh,這會爲我創建表還是我需要在界面生成器中創建tableview?我需要調用這些嗎?對不起,我的無知是新的IOS dev – 2010-12-06 16:48:16

1

作爲一般的方法,你只需要設置相關的控制器類是有問題的UITableView的委託,然後實現以下方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

不過,我建議你閱讀蘋果的Table View Programming guide - 它不僅僅是一個簡單的代碼示例,而且相當容易遵循。

一旦你這樣做了,如果你還在示例代碼之後,只需在UITableView Class reference的「相關示例代碼」部分下載其中一個項目即可。 (如果您在Xcode這樣做蘋果的文檔查看器中它可以自動下載等,將打開在Xcode項目適合你。)

0

首先你要設置的UITableViewDelegate和UITableViewDatasource在.h文件中,其中你正在創建tableview並聲明一個NSarray來存儲表值,在viedidload函數的.m文件中,你需要用對象初始化數組(arr_name = [NSArray alloc] initwith 對象:@「one」,@「two」 ,無),你必須把這三種方法

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分
  • (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath

  • (無效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath