Connecting plist data to table in iPhone

1)Format of the plist:
The global tag is array. In array tag there are dict tags.
In each dict tag there are keys and their corresponding string values.

2)Connecting to a web-Page:
In order to connect the web-page, I used ASIHTTP library.
NSURL *url = [NSURL URLWithString:customURL];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
'startAsynchronous' method is used to gather data from web asynchronously. After all data is loaded, requestFinished method is called.

- (void)requestFinished:(ASIHTTPRequest *)request

Within this method, I created a NSData named plistdata.
NSData *plistData=[request responseData];
After that, I implemented an NSMutableArray:
NSMutableArray *plist;
I also implemented error and format variables but I only use them when serialization.
NSPropertyListFormat format;
NSString *error;
With using NSData, I filled arrays with proper objects:
plist = [NSPropertyListSerialization propertyListFromData: plistData mutabilityOption: NSPropertyListImmutable format:&format errorDescription:&error];
After that, I added object that stored in plist array to my tableData:
[self.tableData addObjectsFromArray:plist];
Finally, I refreshed my tableView to show newly added data.
[tableViewObject reloadData];
3)Showing elements of plist in table cells

The structure of plist allows either array or dictionary. In my plist, there is an array and inside the array there are dictionaries. Each dictionary has two keys and their corresponding string. So that, I implemented a NSMutableDictionary and take its data according to key values in the 'cellForRowAtIndexPath' method of table:
NSMutableDictionary *dictionary=[tableData objectAtIndex:indexPath.row-1];
NSString *heading=[dictionary objectForKey:@"title"];
NSString *tnPath=[dictionary objectForKey:@"tnpath"];
Second key contains an image path. Therefore I loaded the image to table cell's image view with the following code:

[cell.Image setImageWithURL:[NSURL URLWithString:[tnPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ] placeholderImage:[UIImage imageNamed:@"default.png"]];
default.png has already been added to the project as the default image for table cells.

No comments:

Post a Comment