2010-12-06 82 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