Filling a UITableView from Arrays

I need help with the following code. I use hpple to parse html. and I need help using the data.

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

I want to create and populate a table from an array of "headers". Then, can I click an element in a table to load a subtitle that should show the corresponding image in the same index?

I would like to do this programmatically or using IB

can anyone suggest some sample code or tutorial?

Thanks.

+2
source share
3 answers
  - (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 ..*/ } 
+4
source

As a general approach, you just need to set the appropriate controller class as a delegate for the UITableView in question, and then implement the following methods:

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

However, I would recommend reading the "Apple " Table Programming Guide " - it will explain much more than a simple code example, and is reasonably easy to follow.

As soon as you do this, if you are still following the sample code, just load one of the projects in the โ€œRelated Code Sampleโ€ section of the UITableView Class Link . (If you do this in the Apple doc viewer application in Xcode, it will automate the loading, etc. And it will output the project in Xcode for you.)

+1
source

First you have to set UITableViewDelegate and UITableViewDatasource in the .h file where you create the table view and declare NSarray to store the table values, and in the viedidload function .m file you need to initialize an array with objects (arr_name = [NSArray alloc] initwith Objects: @ "one", @ "two", zero), and you should place these three methods

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

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

0
source

Source: https://habr.com/ru/post/956513/


All Articles