iOS:将数组添加到UITableView-显示为一项
我遇到的问题是,我有一个名为:_podAllEntries的NSMutableArray,其中包含url路径。然后将此数组的内容加载到我的UITableView中的一行中。此数组中总共有4项,每个项在表中应有一个条目。下面是该应用程序工作原理的更详细说明: 此iOS应用程序使用NSURL从网站使用XML数据:。从那里,我使用NSXMLParser解析XML,并在enclosure元素中搜索名为url的属性。然后,所有与url匹配的属性都将添加到数组中,然后应在UITableView中加载,每个项目位于单独的行上。如前所述,只有一行已填充 我可能在这一点上遗漏了一些小的东西,在正确的方向上轻推一下或者任何反馈都会非常感激 UnDigParser.h 用于分析类的头文件:iOS:将数组添加到UITableView-显示为一项,ios,uitableview,Ios,Uitableview,我遇到的问题是,我有一个名为:_podAllEntries的NSMutableArray,其中包含url路径。然后将此数组的内容加载到我的UITableView中的一行中。此数组中总共有4项,每个项在表中应有一个条目。下面是该应用程序工作原理的更详细说明: 此iOS应用程序使用NSURL从网站使用XML数据:。从那里,我使用NSXMLParser解析XML,并在enclosure元素中搜索名为url的属性。然后,所有与url匹配的属性都将添加到数组中,然后应在UITableView中加载,每个项
#import <Foundation/Foundation.h>
@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> {
}
@property (retain) NSMutableArray *links;
@end
#import "UnDigParser.h"
@implementation UnDigParser
NSMutableArray *_links;
@synthesize links = _links;
-(void)parserDidStartDocument:(NSXMLParser *)parser{
_links=[[NSMutableArray alloc] init];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"enclosure"]){
NSString *link = [attributeDict objectForKey:@"url"];
if (link){
[_links addObject:link];
}
}
//NSLog(@"%@",_links);
}
-(BOOL)parse{
self.delegate = self;
return [super parse];
}
@end
ViewController.h
您不了解如何在iOS中构建UITableView。 您应该在整个协议中填充UITableView 你应该阅读有关苹果的信息
不要使用InsertRowsatindExpath添加主行。您需要这样做:
[NSString stringWithFormat:@"%@", [_podAllEntries objectAtIndex:indexPath.row]];
例子:
还可以从addRows中删除以下语句,因为cellForRowAtIndexPath:是实际创建单元格的位置
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
在cellForRowAtIndexPath中,而不是[NSString stringWithFormat:@%@,_podAllEntries];您应该有[NSString stringWithFormat:@%@,[\u podAllEntries objectAtIndex:indexath.row]];。这将确保表的每一行都有一个不同的_podAllEntries数组行
乔纳斯说得对,你不应该做插入式的探索。您定义的委托方法负责确保添加所有内容
您的podAllEntries有多少项?它看起来像:[[u podAllEntries insertObject:parser.links atIndex:0];将添加单个对象,该对象本身就是一个数组。因此,您只有一行。我想您需要[\u podAllEntries insertObjectsFromArray:parser.links]
我取下了钥匙[self.tableView insertRowsAtIndexPaths部分,并修改了NSString*cellValue,如上所述。我仍然只有一行填充。此外,我有tableView设置,可以在单击时注销所选行。下面是输出,显示数组中包含的所有项。因此,整个数组仍然被抛出到第一行。如何数组中有y个项?如果只有1个项显示,那么数组中只有1个项。如果数组中应该有更多项,那么您分析的数据是错误的。似乎我分析的数据是错误的,因为url属性的内容正在加载到同一个条目中。是的,因此当您正确分析数据时,请使用方法,您将在tableView中看到所有项目。非常感谢您的帮助!至少我知道该部分已修复,问题在于将项目添加到数组中。我将尝试自己解决该部分。
[NSString stringWithFormat:@"%@", [_podAllEntries objectAtIndex:indexPath.row]];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellValue = [NSString stringWithFormat:@"%@", [_podAllEntries objectAtIndex:indexPath.row]];
cell.textLabel.text = cellValue;
/* to use less code, you can also do it like this: */
//cell.textLabel.text = [_podAllEntries objectAtIndex:indexPath.row];
return cell;
}
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];