0

我正在显示 100 个远程图像tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


//static NSString *CellIdentifier = @"Cell";
NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

}

cell.imageView.image = nil;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;


// Configure the cell...

for (UIView *view in cell.contentView.subviews) {
    if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UILabel class]]||[view isKindOfClass:[UIImageView class]]) {
        [view removeFromSuperview];
    }
}

int imageNumber = 0;

if (isInSearchMode)
{
    PhotoVO *photoVO = (PhotoVO *)[searchResultArray objectAtIndex:indexPath.row];

    UIImageView *photo_View = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, width , height - 10)];
    photo_View.tag = 101;
    [[photo_View layer] setBorderWidth:3.0f];
    [[photo_View layer] setBorderColor:[UIColor whiteColor].CGColor];
    [photo_View setImageWithURL:[NSURL URLWithString:photoVO.thumb_URL1] placeholderImage:[UIImage imageNamed:@"loader"]];


    [cell.contentView addSubview:photo_View];

    UILabel *stringLable=[[UILabel alloc]initWithFrame:CGRectMake(130, 20, 150, 30)];
    stringLable.backgroundColor=[UIColor clearColor];
    stringLable.text=photoVO.photoName;
    stringLable.font=[UIFont systemFontOfSize:16.0];
    [cell.contentView addSubview:stringLable];

    UILabel *tagLable=[[UILabel alloc]initWithFrame:CGRectMake(130, 55, 150, 30)];
    tagLable.backgroundColor=[UIColor clearColor];
    tagLable.text=photoVO.tagString;
    tagLable.font=[UIFont systemFontOfSize:12.0];

    [cell.contentView addSubview:tagLable];


}
else
{

    for (int i = (indexPath.row * imagesCount); i < ((indexPath.row *imagesCount) + imagesCount); i++) {

        if (i < [cellImageVOArray count]) { // If resultsArray Count is odd then we no need to create cell image

            PhotoVO *photoVo = (PhotoVO *)[cellImageVOArray objectAtIndex:i];

            UIButton *appIconBtn = [UIButton buttonWithType:UIButtonTypeCustom];

            appIconBtn.frame = CGRectMake(((imageNumber * 5)+5)+(imageNumber * width), 2, width, height -4);

            appIconBtn.tag = i + 100;
            [[appIconBtn layer] setBorderWidth:3.0f];
            [[appIconBtn layer] setBorderColor:[UIColor whiteColor].CGColor];

            [appIconBtn addTarget:self action:@selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];

            [appIconBtn setBackgroundImageWithURL:[NSURL URLWithString:photoVo.thumb_URL1] placeholderImage:[UIImage imageNamed:@"loader.png"]];

            //[appIconBtn setBackgroundImageWithURL:[NSURL URLWithString:photoVo.thumb_URL1]];

            [cell.contentView addSubview:appIconBtn];
            imageNumber ++;

        }

    }



}

return cell;

}

我正在使用上面的代码在 中显示图像tableView,但是在我检查它的所有方式中都会收到内存警告。我认为每次都会创建单元格,所以如果您在代码中发现任何问题,请告诉我。

4

2 回答 2

5

这是个问题:NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];

您没有重用任何东西,因为您正在为每个单元格创建一个新标识符。可以重复使用几个不同的单元格样式很好,但您只是为每一行创建一个新单元格。

其次,你需要考虑你在这里做什么:

for (UIView *view in cell.contentView.subviews) {
    if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UILabel class]]||[view isKindOfClass:[UIImageView class]]) {
        [view removeFromSuperview];
    }
}

每次需要一个单元时,您都要移除构成单元的部件,然后立即重新制作它们。您应该在UITableView. 您应该考虑创建一个UITableViewCell具有您需要的部分的自定义子类,然后使用它。话虽如此,看起来您只有一个图像和两个默认标签,UITableViewCell因此您可能根本不必创建它们,除非您的单元格非常自定义。

最后,你应该看看你在做什么isInSearchMode。现在你基本上有一个针对整个表的 if 语句。这不是一件可怕的事情,但如果你这样做,你应该有两个单元格标识符,一个用于每个可能的单元格。然后在if语句中只交换单元标识符并填写适当的数据。

最重要的是,如果可能(在您的情况下似乎是这样),您根本不应该在此方法中创建新视图。你应该让UITableViewCell处理。

创建自定义单元格

你从一个简单的UITableViewCell. 然后,您可以为您需要的每个自定义部件添加一个属性,例如UILabelUIImageView。您可以通过覆盖来创建它们init,也可以将它们放入自定义属性获取器中,按需创建它们。

//  CustomCell.h
#import <UIKit/UIKit.h>

@interface Custom : UITableViewCell

@property (strong, nonatomic) UILabel *titleLabel;

@end

//  CustomCell.m
#import "CustomCell.h"

@implementation CustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 10.0, self.contentView.frame.size.width - 24.0, 22.0)];
        [self.titleLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [self.titleLabel setHighlightedTextColor:[UIColor whiteColor]];
        [self.titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
        [self.titleLabel setBackgroundColor:[UIColor clearColor]];
        [self.titleLabel setTextColor:[UIColor blackColor]];
        [self.titleLabel setAdjustsFontSizeToFitWidth:YES];
        [self.titleLabel setMinimumFontSize:8.0];

        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}

@end

然后你只需要重写你cellForRowAtIndexPath:的使用你的自定义类。在您的情况下,您可以有两个自定义单元格并在它们之间切换。这将只按需创建足够的每个单元格,并在它们在屏幕上和屏幕外移动时重复使用它们。

static NSString *CellIdentifier = @"Cell";
static NSString *SearchCellIdentifier = @"SearchCell";

if (isInSearchMode) {
    SearchCell *cell = (SearchCell *)[tableView dequeueReusableCellWithIdentifier:SearchCellIdentifier];

    if (cell == nil) {
        cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.titleLabel = @"Custom Search Title";

} else {
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.titleLabel = @"Custom Title";

}

根据您的应用程序的工作方式,这可以很容易地进一步重构,但这应该会让您走上正确的道路。

于 2013-03-12T12:40:35.947 回答
0

是的,每一行都有不同的单元格标识符,因此不会发生重用。

改变:

NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];

NSString *CellIdentifier = @"CellId";
于 2013-03-12T12:40:51.953 回答