如何发布UITableViewCell的未使用的textLabel?

时间:2015-05-13 16:54:40

标签: ios objective-c uitableview

I've detected that the textLabels of UITableViewCell are using too much memory. How can I release them?

我发现UITableViewCell的textLabel使用了太多内存。我该如何发布它们?

对它显示的字符串使用弱引用并且自动释放池没有帮助,这是我的cellForRowAtIndexPath方法的源代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    @autoreleasepool
    {
        //text
        __weak NSString *text = @"Home";
        cell.textLabel.text = text;

        //image
        cell.imageView.image = nil;

        //load image asynchronous
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
        dispatch_async(queue, ^(void)
                       {

                           NSString *urlStr = @"http://st.vitalk.vn/img/2015/4_13/tulinh_2kjp5i61imfp7_vitalk_9927e8_0x122_max.jpg";
                           NSURL *url = [NSURL URLWithString:urlStr];
                           NSURLRequest *request = [NSURLRequest requestWithURL:url];

                           [NSURLConnection sendAsynchronousRequest:request
                                                              queue:[NSOperationQueue mainQueue]
                                                  completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
                            {
                                UIImage *image = [UIImage imageWithData:data];
                                if (image)
                                {

                                    dispatch_async(dispatch_get_main_queue(), ^{

                                        cell.imageView.image = image;
                                        [cell setNeedsLayout];
                                    });
                                }

                            }];
                       });
    }

    return cell;
}

1 个答案:

答案 0 :(得分:0)

该仪器配置文件中没有任何内容可以解释为内存问题。有很多分配,但标签使用的实际内存只有4.6mb。 UITableView在幕后做了很多事情来减少内存使用量,所以除非你保持对单元格或子视图的明确引用,否则我将分配数量下放到UITableView管理中。

我绝对会删除自动释放池,弱引用和GCD的所有代码,因为它是不必要的,然后只有在您开始注意到应用程序的内存使用等问题时才会查看内存使用情况在应用程序中导航时不断攀爬,或者由于内存使用情况,应用程序开始被操作系统杀死。