2011-02-02 72 views
0

我无法让我的UITableView显示我的解析元素。这是我的代码:如何使用解析元素填充UITableView?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
//elementsArray is a NSArray that is declared in .h 
elementsArray = [document selectElements: @"name"]; 
[myTable reloadData]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return [elementsArray count]; 

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil){ 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [elementsArray objectAtIndex:indexPath.row]; 

    return cell; 

} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

} 

我的NSLog打印出所有的元素,但元素不显示在UITableView中。一切都迷上了罚款和所有代表等。它的工作原理,如果我定义一个静态的NSArray像这样

NSArray * elementsArray = [NSArray arrayWithObjects:@"name1",@"name2",@"name3",nil]; 

我不明白为什么这是行不通的。任何帮助,将不胜感激。谢谢。

+0

你需要为你的“elementsArray”添加一个属性并将其同步,只需将它声明在.h文件夹中即可。也就是说,我没有看到“文件”被宣布。尝试在重新加载表之前记录“elementsArray”的内容,以查看其中的内容。 – Moshe 2011-02-02 02:23:41

回答

0

cellForRowAtIndexPath中设置一个断点,看看elementsArray是否仍然有正确的元素。从你的代码中,不清楚它是如何保留在事件之间的。

0

看来好像你缺少retain这里的电话:

elementsArray = [document selectElements: @"name"]; 

假设selectElements:符合苹果的内存管理准则的实施,这将需要:

elementsArray = [[document selectElements: @"name"] retain];