【问题标题】:App get crashed while loading large amount of data in UITableview在 UITableview 中加载大量数据时应用程序崩溃
【发布时间】:2012-02-07 10:28:02
【问题描述】:

我在 uitableview 单元格中显示超过 10 列并且有超过 100 行,但是当我滚动 tableview 或重新加载表格时,它收到内存警告并且应用程序崩溃。 我们也在这个表格视图上使用折叠和展开。

以下代码应用于 cellforrowatindexpath

for(i=0;i<[appDel.tempItemDic count];i++)
{                  
    UILabel *label = [[UILabel  alloc] initWithFrame:CGRectMake(CNT+30, -30, 350,100)];
    label.font = [UIFont systemFontOfSize:14.0];
    label.text =[tempDic objectForKey:[appDel.arrColumnList objectAtIndex:[[appDel.tempArr objectAtIndex:i]intValue]]];
    label.textAlignment = UITextAlignmentLeft;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor=[UIColor clearColor];
    label.numberOfLines=1;
    [cell.contentView addSubview:label]; 
    [label release];

    backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ListItems_secondLevel(3).png"]];
    cell.backgroundView = backgroundView;
    cell.imageView.image=[UIImage imageNamed:@"arrow.png"];
    CNT+=350;
}

【问题讨论】:

  • 这不是 UITable 的工作方式。这是一篇解释 cell reuse 的博文。

标签: objective-c ios cocoa-touch uitableview


【解决方案1】:

您只需要在创建单元格时创建UILabels。在重用单元格时,您可以使用 tag 获取 UILabel 并将值设置为该值。不要一次又一次地创建。

编辑: 除此之外,正如莎拉所提到的,您不需要循环执行此操作。对于每个单元格都会调用该方法,您可以根据 indexPath 设置值。

【讨论】:

  • 但是,我正在为循环的每次迭代创建新的 uilable 对象,因为假设我们在 tableview 中没有 10 个列,我将不得不创建 10 个新的 uilabel 对象来显示每列的值,所以请告诉我如何处理这种情况。
【解决方案2】:

方法中不需要for loop。正如objectatIndex 一样。删除 for 循环,因为您已经在使用 objectAtIndex,然后重试。

【讨论】:

  • @SONOFGOD :您正在该方法中创建 UILabel ,并且每次都会重新初始化。所以你不需要去“for循环”
  • 但是,我们使用 forloop 来显示列的动态编号以及每个单元格的值。因此,当上面提到的 cellforrowatindex 调用循环运行时,没有列以显示每列的值。
【解决方案3】:

您可能没有重复使用该单元格。因此,如果您创建 100 个单元格并且每个单元格有 10 个标签,那么您将有 UILabel 视图全部保存在内存中。 您应该以这种方式重用单元格(代码未经测试,只是为了给您一个想法,我不是在评论您创建文本的方式,我只关心内存警告):


-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  // add your code here to get the number of columns; I assume you defined a "getNumberOfColumnsForRowAtIndexPath:" somewhere in your code to retrieve this figure
  NSInteger numberOfColumns = [appDel getNumberOfColumnsForRowAtIndexPath:indexPath];
  NSString *cellId=[NSString stringWithFormat:@"MyComplexCellID_%d",numberOfColumns];
  UITableViewCell *cell = [tv dequeReusableCellWithIdentifier:cellId];
  if(!cell) {
    // here you create the cell and add the labels; DON'T GIVE TEXT TO THE LABELS HERE
    // in order to get the right label give them a tag; e.g. 500 is column 0, 501 column 1,... 
    for(int i=0;i<numberOfColumns;i++) {
      UILabel *myLabel = ...
      myLabel.tag=500+i;
    }
  }
  // here you configure the cell
  for(int i=0;i<numberOfColumns;i++) {
    NSString *columnText = retrieve here your column text;
    UILabel *labelColumn = (UILabel *)[cell.contentView viewWithTag:500+i];
    labelColumn.text = columnText;
  }

  return cell;

}

【讨论】:

  • 但这是我的应用程序的功能是我们可以添加动态列数,它们不是固定列数。
  • 您可以根据列数生成自定义ID:1.获取列数“num”:2.创建cellID:cellId = [NSString stringWithFormat:@"MyCellID_%d",num] ; 3.使用那个cellID 你至少会减少重复单元格的数量。占用了更多内存,但不是您现在获得的大量内存。
  • 查看我编辑的答案;现在您最初获得 numberOfColumns (我假设您定义了一个函数来执行此操作),然后根据此数字定义一个特定的 cellId (因此您将根据列数拥有不同类型的单元格),最后您将创建单元格.
【解决方案4】:

如果您在表格视图中加载大量数据,我会建议您

1:-

(void)viewDidLoad {
    [super viewDidLoad];


dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableview reloadData];
});


}

2:-

(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);


dispatch_async(q, ^{

    NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - Main Thread");

    [self methodForServerResponse];
});



}

在那个方法中ForServerResponse-

(void)methodForServerResponse{

  dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);



dispatch_async(q, ^{

    NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - Main Thread within Main Thread");

    //implement what you stuff need from server 
});

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 2015-02-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多