【问题标题】:How to populate UITableView with parsed elements?如何使用解析的元素填充 UITableView?
【发布时间】:2011-02-02 02:17:32
【问题描述】:

我无法让我的 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];

我不明白为什么它不起作用。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 您需要为您的“elementsArray”添加一个属性并合成它,只需在 .h 文件夹中声明它即可。也就是说,我没有看到“文件”被声明了。在重新加载表格之前尝试记录“elementsArray”的内容,看看里面有什么。
  • 我已经添加了属性并合成了它,但仍然没有添加到表中。当我记录它时,应该在表格中显示所有内容。我还尝试将数组保存到文件并读取文件,但仍然没有添加数据。也许我在数组或其他方面做错了什么。身份证
  • 在显示未解析的元素时,您是否检查过 tableview 是否正常工作?您的问题可能不是来自解析。

标签: iphone cocoa-touch uitableview uikit nsarray


【解决方案1】:

cellForRowAtIndexPath中设置断点,看看elementsArray是否还有正确的元素。从您的代码中,尚不清楚它是如何/是否在事件之间保留的。

【讨论】:

    【解决方案2】:

    您似乎在这里错过了对retain 的呼叫:

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

    假设 selectElements: 的实现符合 Apple 的内存管理指南,则需要:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多