Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableView不加载行的NSXmlParser_Iphone_Objective C_Xml_Ios4_Nsxmlparser - Fatal编程技术网

Iphone UITableView不加载行的NSXmlParser

Iphone UITableView不加载行的NSXmlParser,iphone,objective-c,xml,ios4,nsxmlparser,Iphone,Objective C,Xml,Ios4,Nsxmlparser,好的,我正在尝试将XML数据解析到一个表中,这是我目前拥有的代码,它没有在表中的每一行中显示名称,我认为我的代码效率很低,因为我读过两本不同的书,并且尽了最大努力使所有内容都能协同工作。请为我的问题提供代码改进和修复建议谢谢:D我也知道,我还没有发布任何东西,打算稍后再发布:) #导入“firstLevelViewController.h” #定义有趣的标签名称@“文本”,“名称”,无 @firstLevelViewController的实现 @合成SDATA; @综合用户阵列; @合成表阵列;

好的,我正在尝试将XML数据解析到一个表中,这是我目前拥有的代码,它没有在表中的每一行中显示名称,我认为我的代码效率很低,因为我读过两本不同的书,并且尽了最大努力使所有内容都能协同工作。请为我的问题提供代码改进和修复建议谢谢:D我也知道,我还没有发布任何东西,打算稍后再发布:)

#导入“firstLevelViewController.h”
#定义有趣的标签名称@“文本”,“名称”,无
@firstLevelViewController的实现
@合成SDATA;
@综合用户阵列;
@合成表阵列;
-(无效)viewDidLoad
{
self.userArray=[[NSMutableArray alloc]init];
interestingTags=[[NSSet alloc]initWithObjects:INTERESTING_TAG_NAMES];
self.title=@“测试”;
[推特数据发布];
tweetsData=[[NSMutableData alloc]init];//alloc xml的持有者可能很大,所以我们使用NSMutableData类型
NSURL*url=[NSURL URLWithString:@”http://twitter.com/statuses/public_timeline.xml“];//要下载的url字符串
NSURLRequest*request=[[NSURLRequest alloc]initWithURL:url];//使用url设置请求
NSURLConnection*connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];//从下面启动连接并调用连接方法
[连接释放];
[请求释放];
}
//多次打电话
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
NSLog(“DidReceiveData连接”);
[tweetsData appendData:data];//将数据从方法调用行追加到tweetsData tweetsData现在保存xml文件
}
//下载完成后调用
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
NSLog(@“完成”);
[self startParsingTweets];
}
//*****************************启动解析器代码************************************
-(无效)startParsingTweets
{
NSLog(@“解析初始化”);
NSXMLParser*tweetParser=[[NSXMLParser alloc]initWithData:tweetsData];//使用NSMutableData数据类型进行解析
tweetParser.delegate=self;//将委托设置为此viewControlelr
[推特解析];
[推特解析器发布];
}
//解析文档时调用
-(void)parserdistartdocument:(NSXMLParser*)解析器
{
NSLog(@“已开始解析”);
[tweetsString release];//确保其为空以清除以前的任何数据
tweetsString=[[NSMutableString alloc]initWithCapacity:(20*(140+20))];/(呼叫数*(tweet的大小+用户名)
currentElementName=nil;
currentText=nil;
}
//这是为每个xml元素调用的
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName属性:(NSDictionary*)attributeDict
{
NSLog(@“已启动元素”);
if([elementName IsequalString:@“status”])//if elementName==status然后开始新tweet,因此创建新字典
{
[currentTweetDict发布];
currentTweetDict=[[NSMutableDictionary alloc]initWithCapacity:[interestingTags count]];//使用两个部分创建字典
}
else if([interestingTags containsObject:elementName])//如果当前元素是存储在InterestingTag中的元素,请保留elementName并创建一个新字符串来保存其值
{
currentElementName=elementName;//保留当前元素名称
currentText=[[NSMutableString alloc]init];
}
}
-(void)解析器:(NSXMLParser*)解析器查找字符:(NSString*)字符串
{
NSLog(“附加”);
[currentText appendString:string];
}
//在每个元素之后,它在调用此方法后返回到父元素
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName
{
if([elementName IsequalString:currentElementName])
{
[currentTweetDict设置值:currentText-forKey:currentElementName];
}
else if([elementName IsequalString:@“status”])
{
[self.userArray addObject:currentTweetDict];
//最终放在表中只是暂时测试
}
[当前文本发布];
currentText=nil;
}
-(void)parserDidEndDocument:(NSXMLParser*)解析器
{

对于(int i=0;i,在数据准备好显示在表视图上之后,应调用
reloadData
,以再次调用
UITableView
委托方法

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    for(int i=0;i<[self.userArray count];i++)
    {
        NSDictionary *rowData = [self.userArray objectAtIndex:i];
        NSString *textName = [[NSString alloc] initWithFormat:@"user: %@", [rowData objectForKey:@"name"]];
        [self.tableArray addObject:textName];
    }

        NSLog(@"done");

     //reloadData so that the table view delegate get called again
     [self.tableView reloadData];
}
-(void)parserdinddocument:(NSXMLParser*)解析器
{

对于(int i=0;i关于将数据添加到表中,我的做法是否正确。是的,您在
cellforrowatinexpath
中的做法似乎是正确的。您还使用了
dequeueReusableCellWithIdentifier
,这对性能至关重要。您应该在为c设置文本后才释放
textName
嗯,当您分配字符串时,可能会出现内存泄漏,并且永远不会释放它。此外,还可以在初始化时将单元格设置为自动释放:
[cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelIdentifier]autorelease]
是的,我已经这样做了,遇到了一个问题…当查看我的表格时,我可以向下滚动而没有任何问题,但是如果我再次向上滚动,应用程序崩溃,没有向控制台报告任何内容。woops nevermind我修复了它,在CellForRowAtIndexPath:D中有[rowData release]
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    for(int i=0;i<[self.userArray count];i++)
    {
        NSDictionary *rowData = [self.userArray objectAtIndex:i];
        NSString *textName = [[NSString alloc] initWithFormat:@"user: %@", [rowData objectForKey:@"name"]];
        [self.tableArray addObject:textName];
    }

        NSLog(@"done");

     //reloadData so that the table view delegate get called again
     [self.tableView reloadData];
}