2010-12-06 28 views
2

J'ai besoin d'aide pour le code suivant J'utilise hpple pour analyser html. et j'ai besoin d'aide pour utiliser les données.Remplissage d'un UITableView à partir de tableaux

-(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]; 

je veux créer et remplir une table à partir du tableau « titres » ensuite être en mesure de cliquer sur l'élément sur la table pour charger une sous-vue qui devrait montrer l'artical correspondant au même indice?

Je voudrais faire programatically ou en utilisant IB

peut-on suggérer quelques exemples de code ou un tutoriel?

Merci.

Répondre

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

Thats great rajesh, cela va-t-il créer la table pour moi ou dois-je créer la vue de table ceci dans le constructeur d'interface? Ai-je besoin d'appeler ceux-ci? désolé pour mon ignorance im nouveau à IOS dev –

+0

pas vous devez créer une vue de la table dans didload. – rajesh

+1

vous n'avez pas besoin de créer une vue de table dans didload. Dans votre fichier .h, mettez UITbaleView * yourtableView; et dans le fichier .m sous la méthode viewDidLoad mettez yourtableView = [[UITableView alloc] initWithFrame: CGRectMake: (mettez vos propres coordonnées)]; puis yourtableView.delegate = self; alors [self.view addSubView: yourTableView]; et int ma réponse UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: MyIdentifier]; remplacez le 'tableView' avec yourTableView. k? s'il vous plaît laissez-moi savoir si ny questions? – rajesh

1

Comme une approche générale, il vous suffit de définir la classe du contrôleur concerné en tant que délégué pour la UITableView en question et mettre en œuvre les méthodes suivantes:

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

Cependant, je vous recommande la lecture d'Apple Table View Programming guide - il va comprendre beaucoup plus qu'un simple exemple de code, et il est raisonnablement facile à suivre.

Une fois que vous avez fait cela, si vous êtes toujours après l'exemple de code, téléchargez simplement l'un des projets dans la section "Exemple de code associé" du UITableView Class reference. (Si vous faites cela dans la visionneuse de document d'Apple Xcode il automatise le téléchargement, etc., et apportera le projet Xcode pour vous.)

0

D'abord, vous devez définir UITableViewDelegate et UITableViewDatasource dans le fichier .h où vous créez une vue table et déclarez un NSarray pour stocker les valeurs de la table, et dans le fichier .m de la fonction viedidload vous devez initialiser le tableau avec des objets (arr_name = [NSArray alloc] initwith Objets: @ "un", @ "deux" , nul) et vous devez placer ces trois méthodes

  • (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section
  • (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath

  • (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath