1

我有一个Parser对象,其中包含来自 Internet 的 15 个项目(文章)。

我正在尝试将这些项目加载到我的TableView. 我的问题是我在开始时有 8 个项目可见(4 英寸视网膜模拟器)但是当它开始滚动时,我几乎所有的内容都丢失了,我看不到其余的 7 个项目。不知道我做错了什么,这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    parser = [[Parser alloc] init];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[parser items] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ArticleCell"];
    Article *article = [parser items][indexPath.row];

    cell.title.text = article.title;
    cell.date.text = article.date;

    return cell;
}

编辑:

如果我在返回单元格之前记录数据,这就是滚动时显示的内容cellRowAtIndexPath

2013-09-29 13:37:05.341 Inter[3685:a0b] Article for index: 0 . Title: Cagliari-Inter, tutte le curiosità
 ; date: (null) .
2013-09-29 13:37:05.343 Inter[3685:a0b] Article for index: 1 . Title: Cerrone "Oggi abbiamo vinto da squadra"
 ; date: (null) .
2013-09-29 13:37:05.344 Inter[3685:a0b] Article for index: 2 . Title: Le immagini del 10° "Memorial Prisco"
 ; date: (null) .
2013-09-29 13:37:05.345 Inter[3685:a0b] Article for index: 3 . Title: Primavera, Udinese-Inter 0-1
 ; date: (null) .
2013-09-29 13:37:05.345 Inter[3685:a0b] Article for index: 4 . Title: Tutte le immagini della vigilia di Cagliari-Inter
 ; date: (null) .
2013-09-29 13:37:05.346 Inter[3685:a0b] Article for index: 5 . Title: Udinese-Inter Primavera, 0-0 a fine primo tempo
 ; date: (null) .
2013-09-29 13:37:05.347 Inter[3685:a0b] Article for index: 6 . Title: Mazzarri "Rischio buccia di banana in un momento di euforia"
 ; date: (null) .
2013-09-29 13:37:05.348 Inter[3685:a0b] Article for index: 7 . Title: Inter Campus in Bosnia Erzegovina: passi avanti per i progetti a Sarajevo e Domanovici
 ; date: (null) .
2013-09-29 13:37:11.053 Inter[3685:a0b] Article for index: 8 . Title: (null) ; date: (null) .
2013-09-29 13:37:28.181 Inter[3685:a0b] Article for index: 9 . Title: (null) ; date: (null) .
2013-09-29 13:37:29.499 Inter[3685:a0b] Article for index: 10 . Title: (null) ; date: (null) .
2013-09-29 13:37:29.591 Inter[3685:a0b] Article for index: 11 . Title: (null) ; date: (null) .
2013-09-29 13:37:35.642 Inter[3685:a0b] Article for index: 1 . Title: (null) ; date: (null) .
2013-09-29 13:37:35.767 Inter[3685:a0b] Article for index: 0 . Title: (null) ; date: (null) .

编辑:

完整的代码在这里

4

7 回答 7

3

尝试从 (nonatomic, weak) 更改属性

NSMutableString *title; 
NSMutableString *date;

to (nonatomic, strong) 应该可以解决您的问题。

于 2013-09-29T10:59:13.703 回答
1
 -(void) ViewDidLoad {
  [super ViewDidLoad];
    parser = [Parser alloc] init];

您重新分配并重新初始化了解析器,您正在重新加载数据的代码中是否有任何部分?如果不是,您的数据存储将是空的。

于 2013-09-29T08:59:45.393 回答
1

我的建议:

  1. 请检查您的 tableViewdelegate是否dataSource设置正确。我在你的代码中没有看到。
  2. 请检查,什么单元格类注册到您的表格视图。我的意思是,您需要使用 tableViewregisterClass:forCellReuseIdentifier:registerNib:forCellReuseIdentifier:其他地方。

无论如何,我习惯于在没有 IB 或情节提要的情况下使用 GUI(所以,我正在以编程方式进行所有操作),因此您可能不需要这样做。

祝你好运。

于 2013-09-29T10:14:23.837 回答
0

我以前也有类似的问题。我的问题的原因是我的视图控制器在我滚动我的表格视图后被释放,你能检查一下你是否有同样的原因吗?

于 2013-09-29T08:18:18.847 回答
0

尝试执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * MyIdentifier = @"ArticleCell";
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier: MyIdentifier];
    Article *article = [parser items][indexPath.row];

    cell.title.text = article.title;
    cell.date.text = article.date;

    return cell;
}
于 2013-09-29T08:54:17.057 回答
0

问题是使用的标识符应该是静态字符串

所以声明一个标识符,它是 statisc 并分配它

static NSString *cellId = @"cellIdentifier";    
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier: cellId];

还要确保情节提要单元具有相同的标识符值。即这里的“cellIdentifier”

于 2013-09-29T08:59:20.627 回答
0

检查滚动时 [[parser items] count] 会发生什么。我的猜测是它会减少,这会减少表中的行数。

我在这里可能错了,因为我不知道 Parser 是什么(可能是您的自定义类?)但是如果它用于解析 HTTP 响应,那么解析完成后项目计数可能会减少?

要检查解析器项目计数,请将以下内容添加到您的 tableView:cellForRowAtIndexPath:

NSLog(@"解析器项目数:%d", parser.items.count);

于 2013-09-29T09:02:51.277 回答