0

这是我用来显示我的 tableview 自定义单元格的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"CustomCell";

CustomeCellHome *cell = (CustomeCellHome *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomeCellHome" owner:self options:nil];
    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (CustomeCellHome *) currentObject;
            break;
        }
    }
}

    NSString *strImgUrl = [[[[myarray valueForKey:@"mykey1"]objectAtIndex:indexPath.row] valueForKey:@"url"] objectAtIndex:0];
    [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imgView];
    cell.imgView.imageURL = [NSURL URLWithString:strImgUrl];

    cell.lbl1.text = [[myarray valueForKey:@"mykey2"]objectAtIndex:indexPath.row];
    cell.lbl2.text = [[myarray valueForKey:@"mykey3"]objectAtIndex:indexPath.row];

    cell.lbl3.text = [[myarray valueForKey:@"mykey3"]objectAtIndex:indexPath.row];
    cell.lbl4.text = [[myarray valueForKey:@"mykey4"]objectAtIndex:indexPath.row];

    cell.lbl5.text = [[myarray valueForKey:@"mykey5"]objectAtIndex:indexPath.row];
    cell.lbl6.text = [[myarray valueForKey:@"mykey6"]objectAtIndex:indexPath.row];
}

CustomeCellHome 是 UITableViewCell 的子类,我正在使用 Nib 来显示它。CustomeCellHome 拥有所有的 IBOutlet 并连接到 Nib。所有 IBOutlet 都是属性和综合,我已经在 dealloc 方法中发布了它。

我一次在 tableview 中显示 50 条记录。如果用户想查看更多记录,他可以通过单击下一个按钮来查看,也可以通过单击上一个按钮查看上一个记录。

当用户按下下一个按钮时,我正在调用 Web 服务并从服务器获取下一个 50 条记录。我将之前的 50 条记录保存在数组中,并将这些新记录添加到该数组中。但是在“myarray”中,我一次只保留 50 条以显示在 tableview 中(意味着我一次只显示 50 条记录)。

当我到达查看 850 到 900 或接近该记录时,我的表格视图被拉伸并且它会在一段时间后停止并显示。

同样在查看 1450 到 1500 或接近该记录后,我收到内存警告并且我的应用程序崩溃了。我没有得到这个。为什么会这样?

我用内存工具检查了我没有任何内存泄漏。我是否使用错误来显示自定义单元格或我的 Web 服务调用会导致更多内存?

请任何人都可以指导我。提前致谢。

4

1 回答 1

0

It sounds like you are consuming too much memory. You don't need to leak memory to have your app shot by the watchdog.

I would profile the memory consumption of your app as you load more and more data. You'll probably want to respond to a memoryWarning message and delete all of your objects except for the current set. You could (de)serialize the objects to disc to preserve user's bandwidth.

You don't show the lifespan of your objects, but assuming they are fetched/stored in your UI(Table)ViewController you'll want to implement code in - (void)didReceiveMemoryWarning or - (void)viewDidUnload (iOS5 vs. iOS6) to clear out the older data.

SideNote: is myarray actually a dictionary? It's good practice to give your variables descriptive names to make your code easier to understand (for others, and yourself in 2 months when you need to fix a bug).

于 2012-11-08T20:14:39.237 回答