我如何在 UItableview 单元格中显示 UIlabel,其中 UILabel 包含 xml 数据?
这是我的解析器部分,我从 xml 文件中获取属性和值。现在,当它同时解析 xml 时,应该在表视图单元格中一一显示属性及其值。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if([elementName isEqualToString:@"ProductData"])
{
objectsArray = [[NSMutableArray alloc] init];
productDict = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@ %@", currentElement,string);
if(!currentString){
currentString = [[NSMutableString alloc] init];
}
[currentString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if([elementName isEqualToString:@"id"])
{
[productDict setObject:currentString forKey:@"id"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"productNumber"])
{
[productDict setObject:currentString forKey:@"productNumber"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"name"])
{
[productDict setObject:currentString forKey:@"name"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"dateCreated"])
{
[productDict setObject:currentString forKey:@"dateCreated"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"image"])
{
[productDict setObject:currentString forKey:@"image"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"ProductData"])
{
[objectsArray addObject:productDict];
[productDict release],productDict = nil;
}
[currentString release], currentString = nil;
}
在我的表格视图下方,我想在其中显示 xml 数据。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [objectsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
}
productDict = [objectsArray objectAtIndex:indexPath.row];
NSString *data = nil;
for (NSString *key in [productDict allKeys] ) {
// [data stringByAppendingFormat:@"%@ %@",key,[productDict objectForKey:key]];
data = [NSString stringWithFormat:@"%@%@", key,[productDict objectForKey:key]];
NSLog(@"data: %@",data);
}
cell.textLabel.text = data;
return cell;
[data release];
}
这是这个视图控制器的控制台
2011-10-16 15:19:01.627 cmsCommander[1795:207] namestring: emon
2011-10-16 15:19:01.630 cmsCommander[1795:207] username: [email protected]
2011-10-16 15:19:01.874 cmsCommander[1795:207] found file and started parsing
2011-10-16 15:19:01.877 cmsCommander[1795:207] current Element id : 1
2011-10-16 15:19:01.879 cmsCommander[1795:207] current Element productNumber : a91cc0f4c7
2011-10-16 15:19:01.880 cmsCommander[1795:207] current Element name : Product 1
2011-10-16 15:19:01.881 cmsCommander[1795:207] current Element image : 5e928bbae358c93caedf6115fa7d178b.jpg
2011-10-16 15:19:01.882 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.883 cmsCommander[1795:207] current Element id : 2
2011-10-16 15:19:01.888 cmsCommander[1795:207] current Element productNumber : d8287e2e51
2011-10-16 15:19:01.892 cmsCommander[1795:207] current Element name : Product 2
2011-10-16 15:19:01.893 cmsCommander[1795:207] current Element image : 8bbd8dfff3cdd28285d07810a4fe7c32.jpg
2011-10-16 15:19:01.895 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.896 cmsCommander[1795:207] current Element id : 3
2011-10-16 15:19:01.897 cmsCommander[1795:207] current Element productNumber : 7931c08c22
2011-10-16 15:19:01.897 cmsCommander[1795:207] current Element name : Product 3
2011-10-16 15:19:01.902 cmsCommander[1795:207] current Element image : e19becad20d6f4378e37313c5dbdf070.jpg
2011-10-16 15:19:01.904 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.904 cmsCommander[1795:207] all done!
2011-10-16 15:19:01.906 cmsCommander[1795:207] The number of row in the object array: 1
2011-10-16 15:19:01.909 cmsCommander[1795:207] data: name Product 3
2011-10-16 15:19:01.910 cmsCommander[1795:207] data: id 3
2011-10-16 15:19:01.911 cmsCommander[1795:207] data: productNumber 7931c08c22
2011-10-16 15:19:01.912 cmsCommander[1795:207] data: image e19becad20d6f4378e37313c5dbdf070.jpg
2011-10-16 15:19:01.913 cmsCommander[1795:207] data: **dateCreated 2011-10-06T16:08:45**
,现在我想你已经得到了我的问题。
this is my parser section where i am getting the attribute and values from the xml file.now when it will parse the xml concurrently should show the attribute and its value in the table view cell one bye one.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if([elementName isEqualToString:@"ProductData"])
{
objectsArray = [[NSMutableArray alloc] init];
productDict = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@ %@", currentElement,string);
if(!currentString){
currentString = [[NSMutableString alloc] init];
}
[currentString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if([elementName isEqualToString:@"id"])
{
[productDict setObject:currentString forKey:@"id"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"productNumber"])
{
[productDict setObject:currentString forKey:@"productNumber"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"name"])
{
[productDict setObject:currentString forKey:@"name"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"dateCreated"])
{
[productDict setObject:currentString forKey:@"dateCreated"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"image"])
{
[productDict setObject:currentString forKey:@"image"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"ProductData"])
{
[objectsArray addObject:productDict];
[productDict release],productDict = nil;
}
[currentString release], currentString = nil;
}
below my tableview where i want to show the xml data.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [objectsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
}
productDict = [objectsArray objectAtIndex:indexPath.row];
NSString *data = nil;
for (NSString *key in [productDict allKeys] ) {
// [data stringByAppendingFormat:@"%@ %@",key,[productDict objectForKey:key]];
data = [NSString stringWithFormat:@"%@%@", key,[productDict objectForKey:key]];
NSLog(@"data: %@",data);
}
cell.textLabel.text = data;
return cell;
[data release];
}
This the console of this view controller
2011-10-16 15:19:01.627 cmsCommander[1795:207] namestring: emon
2011-10-16 15:19:01.630 cmsCommander[1795:207] username: [email protected]
2011-10-16 15:19:01.874 cmsCommander[1795:207] found file and started parsing
2011-10-16 15:19:01.877 cmsCommander[1795:207] current Element id : 1
2011-10-16 15:19:01.879 cmsCommander[1795:207] current Element productNumber : a91cc0f4c7
2011-10-16 15:19:01.880 cmsCommander[1795:207] current Element name : Product 1
2011-10-16 15:19:01.881 cmsCommander[1795:207] current Element image : 5e928bbae358c93caedf6115fa7d178b.jpg
2011-10-16 15:19:01.882 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.883 cmsCommander[1795:207] current Element id : 2
2011-10-16 15:19:01.888 cmsCommander[1795:207] current Element productNumber : d8287e2e51
2011-10-16 15:19:01.892 cmsCommander[1795:207] current Element name : Product 2
2011-10-16 15:19:01.893 cmsCommander[1795:207] current Element image : 8bbd8dfff3cdd28285d07810a4fe7c32.jpg
2011-10-16 15:19:01.895 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.896 cmsCommander[1795:207] current Element id : 3
2011-10-16 15:19:01.897 cmsCommander[1795:207] current Element productNumber : 7931c08c22
2011-10-16 15:19:01.897 cmsCommander[1795:207] current Element name : Product 3
2011-10-16 15:19:01.902 cmsCommander[1795:207] current Element image : e19becad20d6f4378e37313c5dbdf070.jpg
2011-10-16 15:19:01.904 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.904 cmsCommander[1795:207] all done!
2011-10-16 15:19:01.906 cmsCommander[1795:207] The number of row in the object array: 1
2011-10-16 15:19:01.909 cmsCommander[1795:207] data: name Product 3
2011-10-16 15:19:01.910 cmsCommander[1795:207] data: id 3
2011-10-16 15:19:01.911 cmsCommander[1795:207] data: productNumber 7931c08c22
2011-10-16 15:19:01.912 cmsCommander[1795:207] data: image e19becad20d6f4378e37313c5dbdf070.jpg
2011-10-16 15:19:01.913 cmsCommander[1795:207] data: **dateCreated 2011-10-06T16:08:45**
now i think you have got my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)