sample xml file :
<fruitName1>Apple</fruitName1>
<pageNo1>2</pageNo1>
<fruitName2>Avacado</fruitName2>
<pageNo2>5</pageNo2>
<fruitName3>Apricot</fruitName3>
<pageNo3>8</pageNo3>
<fruitName4>Banana</fruitName4>
<pageNo4>10</pageNo4>
code:
NSString *string=[[NSBundle mainBundle]pathForResource:@"FruitsName" ofType:@"xml"];
NSData *data=[[NSData alloc]initWithContentsOfFile:string];
NSString* docString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableArray *newArray=[[NSMutableArray alloc]init];
NSArray *lines = [docString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\r\n"]];
for (int i=1;i<=[lines count]/2; i++) {
NSArray *Str=[docString componentsSeparatedByString:[NSString stringWithFormat:@"</fruitName%d>",i]];
NSArray *Strg=[docString componentsSeparatedByString:[NSString stringWithFormat:@"</pageNo%d>",i]];
NSString *news1=[Str objectAtIndex:0];
NSString *news2=[Strg objectAtIndex:0];
NSArray *Str1=[news1 componentsSeparatedByString:[NSString stringWithFormat:@"<fruitName%d>",i]];
NSArray *Str2=[news2 componentsSeparatedByString:[NSString stringWithFormat:@"<pageNo%d>",i]];
[newArray addObject:[Str1 objectAtIndex:1]];
[newArray addObject:[Str2 objectAtIndex:1]];
self.myArray = newArray;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [myArray count]/2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [myArray objectAtIndex:indexPath.row*2];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *pageNoa = [myArray objectAtIndex:indexPath.row*2+1];
[readerViewController navigatePageToIndex:[pageNoa intValue]];
}
<fruitName1>Apple</fruitName1>
<pageNo1>2</pageNo1>
<fruitName2>Avacado</fruitName2>
<pageNo2>5</pageNo2>
<fruitName3>Apricot</fruitName3>
<pageNo3>8</pageNo3>
<fruitName4>Banana</fruitName4>
<pageNo4>10</pageNo4>
code:
NSString *string=[[NSBundle mainBundle]pathForResource:@"FruitsName" ofType:@"xml"];
NSData *data=[[NSData alloc]initWithContentsOfFile:string];
NSString* docString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableArray *newArray=[[NSMutableArray alloc]init];
NSArray *lines = [docString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\r\n"]];
for (int i=1;i<=[lines count]/2; i++) {
NSArray *Str=[docString componentsSeparatedByString:[NSString stringWithFormat:@"</fruitName%d>",i]];
NSArray *Strg=[docString componentsSeparatedByString:[NSString stringWithFormat:@"</pageNo%d>",i]];
NSString *news1=[Str objectAtIndex:0];
NSString *news2=[Strg objectAtIndex:0];
NSArray *Str1=[news1 componentsSeparatedByString:[NSString stringWithFormat:@"<fruitName%d>",i]];
NSArray *Str2=[news2 componentsSeparatedByString:[NSString stringWithFormat:@"<pageNo%d>",i]];
[newArray addObject:[Str1 objectAtIndex:1]];
[newArray addObject:[Str2 objectAtIndex:1]];
self.myArray = newArray;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [myArray count]/2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [myArray objectAtIndex:indexPath.row*2];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *pageNoa = [myArray objectAtIndex:indexPath.row*2+1];
[readerViewController navigatePageToIndex:[pageNoa intValue]];
}
Thanks for taking time for sharing this article, it was excellent and very informative. Its really very useful of all of users. I found a lot of informative stuff in your article. For more visit: iphone development kit
ReplyDelete