Tom Kelly
Tom Kelly

Reputation: 509

Populating a UITableView from arrays

I need help with the following code i am using hpple to parse html. and i need help utilizing 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 the array "titles" Then be able to click the item on the table to load a subview that should show the corresponding artical at the same index?

I'd like to do this programatically or using IB

can anyone suggest some sample code or a tutorial?

Thanks.

Upvotes: 2

Views: 10318

Answers (3)

rakesh
rakesh

Reputation: 73

First you have to set UITableViewDelegate and UITableViewDatasource in .h file where you are creating tableview and declare an NSarray to store the table values,and in .m file of viedidload function you need to intialise the array with objects (arr_name=[NSArray alloc]initwith Objects:@"one",@"two",nil) and you have to 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

Upvotes: 0

John Parker
John Parker

Reputation: 54445

As a general approach, you simply need to set the relevant 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'd recommend reading Apple's Table View Programming guide - it'll elucidate a lot more than a simple code sample, and is reasonably easy to follow.

Once you've done that, if you're still after sample code simply download one of the projects within the "Related sample code" section of the UITableView Class reference. (If you do this within Apple's doc viewer in Xcode it automates the download, etc. and will bring up the project in Xcode for you.)

Upvotes: 1

rajesh
rajesh

Reputation: 582

    - (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 ..*/

}

Upvotes: 4

Related Questions