为什么我的桌面视图在第4个条目中断?

时间:2012-03-15 15:37:18

标签: iphone objective-c uiimage

这是我的代码。它适用于前3个条目,然后在第4个条目,它开始显示与其他单元格相同的单元格内容,尽管每个单元格的高度是正确的。任何人都可以发现我在这里看不到的任何问题吗? cell.textLabel.text纯粹用于测试目的,并返回正确的单元格编号,即使单元格的内容不正确。由于它与内容同时设置,这使我相信问题不在cellForRowAtIndexPath中。

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

    static NSString *CellIdentifier = @"Cell";

    JCell *cell = (JCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UIImageView *imageView;
    if (cell == nil) {
        cell = [[JCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeCenter;

        [cell addSubview:imageView];

    }

    UIImage *image = [self.mediaDelegate largeThumbnailForMediaAtIndex:indexPath.row];
    imageView.frame = CGRectMake(0, IMAGE_SPACING, image.size.width, image.size.height);
    imageView.image = image;
    imageView.center = CGPointMake(cell.bounds.size.width / 2, imageView.center.y);

    cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    int mediaHeight = [self.mediaDelegate heightForLargeThumbnailForMediaAtIndex:indexPath.row];    

    return mediaHeight + (IMAGE_SPACING * 2);
}

它总是会成为一个JImage,因为我还没有开始使用电影支持。

-(UIImage *)largeThumbnailForMediaAtIndex:(int)index
{
    id media = [self.media objectAtIndex:index];
    if ([media isKindOfClass:[JImage class]]) {
        JImage *image = media;
        return [image getLargeThumbnail];
    }
    else if ([media isKindOfClass:[JMovie class]]) {
        JMovie *movie = media;
        return [movie getLargeThumbnail];
    }
    else {
        return nil;
    }
}

-(UIImage *)getLargeThumbnail {
    if (self.largeThumbnail == nil) {
        UIImage *originalImage = [UIImage imageWithContentsOfFile:self.originalImage];
        UIImage *resizedImage = [originalImage imageScaledToFitSize:LARGE_THUMBNAIL_SIZE];
        self.largeThumbnail = @"Generating";
        dispatch_queue_t myQueue = dispatch_queue_create("LargeThumbnailQueue", 0);
        dispatch_async(myQueue, ^{
            NSString *filePath = [JImage writeImageToFile:resizedImage];
            self.largeThumbnail = filePath;
        });

        return resizedImage;

    }
    else if ([_largeThumbnail isEqualToString:@"Generating"]) {
        UIImage *originalImage = [UIImage imageWithContentsOfFile:self.originalImage];
        UIImage *resizedImage = [originalImage imageScaledToFitSize:LARGE_THUMBNAIL_SIZE];
        return resizedImage;
    }
    else {
        NSString *filePath = self.largeThumbnail;
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        return image;
    }

}


+(NSString *)writeImageToFile:(UIImage *)image {

    NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0);

    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = NO;
    BOOL directoryExists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    if (directoryExists) {
    } else {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
    }

    NSString *retinaSupport;
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
        retinaSupport = @"@2x";
    }

    NSString *name = [NSString stringWithFormat:@"%@%@.jpg", [JMedia generateUuidString], retinaSupport];
    NSString *filePath = [path stringByAppendingPathComponent:name];
    [fullImageData writeToFile:filePath atomically:YES];

    return filePath;
}

非常感谢您对此问题的任何帮助或帮助。

3 个答案:

答案 0 :(得分:2)

当你重复使用一个单元格时(即你没有输入if(cell == nil)你没有imageView.imageView将是nil。

在重复使用单元格时添加标记以检索该imageView:

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

        imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeCenter;
        imageView.tag = 1234;
        [cell addSubview:imageView];

    }
    imageView = [cell viewWithTag:1234];

    UIImage *image = [self.mediaDelegate largeThumbnailForMediaAtIndex:indexPath.row];
    imageView.frame = CGRectMake(0, IMAGE_SPACING, image.size.width, image.size.height);
    imageView.image = image;
    imageView.center = CGPointMake(cell.bounds.size.width / 2, imageView.center.y);

    cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];
    return cell;
}

答案 1 :(得分:0)

你不应该做

UIImage *image = [self.mediaDelegate largeThumbnailForMediaAtIndex:indexPath.row];
cell.imageView.frame = CGRectMake(0, IMAGE_SPACING, image.size.width, image.size.height);
cell.imageView.image = image;
cell.imageView.center = CGPointMake(cell.bounds.size.width / 2, imageView.center.y);

(或者至少,您应该从单元格中检索imageView,而不是使用它看起来像实例变量的内容)

答案 2 :(得分:0)

如果进入

,则只将imageView分配给单元格

if (cell == nil)

块。因此,在tableview创建了几个实例后,它将重用它们,并且不会添加imageView。

编辑:

UIImageView *imageView;        <------------ imageView is nil
if (cell == nil) {
  cell = [[JCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  imageView = [[UIImageView alloc] init];
  imageView.contentMode = UIViewContentModeCenter;

  [cell addSubview:imageView];

}

UIImage *image = [self.mediaDelegate largeThumbnailForMediaAtIndex:indexPath.row];
// At this point, imageView is still nil - not been initialised to anything
imageView.frame = CGRectMake(0, IMAGE_SPACING, image.size.width, image.size.height);
imageView.image = image;
imageView.center = CGPointMake(cell.bounds.size.width / 2, imageView.center.y);