合适的viewcell问题

发布于 2024-08-12 19:56:53 字数 1766 浏览 6 评论 0原文

我正在使用自定义单元格来显示图像。但是,当我将图像添加到第一行时,它会每隔第四行加载一次。可能是什么问题?我使用 uiimagepickercontroller 从 iphone 照片库中选取图像,并将其提供给表格的第一个单元格。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    CGSize newSize = CGSizeMake(80, 80);
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imageView.image = newImage;

    [picker dismissModalViewControllerAnimated:YES];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
        for (id currentObject in nib){
            if ([currentObject isKindOfClass:[CustomCell class]]){
                cell = (CustomCell *)currentObject;
                //[cell loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
                break;
            }
        }
    }

    NSUInteger r= [indexPath row];
    NSUInteger s= [indexPath section];
    //[cell setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];
    // NSInteger r= indexPath.row;
    if(r == 1)
        if (s== 0){
             UIImage *img=imageView.image;
             cell.imageView.image = img;
        }
     return cell;
}

i'm using the customised cell to Show an image. But, when i add an image to the first row, its get being loaded every 4th row. What might be the problem? I'm picking the image from the iphone photo library using uiimagepickercontroller and is giving it to the first cell of the table.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    CGSize newSize = CGSizeMake(80, 80);
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imageView.image = newImage;

    [picker dismissModalViewControllerAnimated:YES];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
        for (id currentObject in nib){
            if ([currentObject isKindOfClass:[CustomCell class]]){
                cell = (CustomCell *)currentObject;
                //[cell loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
                break;
            }
        }
    }

    NSUInteger r= [indexPath row];
    NSUInteger s= [indexPath section];
    //[cell setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];
    // NSInteger r= indexPath.row;
    if(r == 1)
        if (s== 0){
             UIImage *img=imageView.image;
             cell.imageView.image = img;
        }
     return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼藏柔 2024-08-19 19:56:53

iPhone SDK UITableView 通过重用不在视图中的单元格而不是始终重新创建新单元格来优化 UITableViewCell 创建。这是通过调用 dequeueReusableCellWithIdentifier: 来完成的。

tableView:cellForRowAtIndexPath: 中,您需要确保出队的单元格被清除,以便它只包含新的单元格内容。在这种情况下,在 if (r == 1) ... 语句之前添加 cell.imageView.image = nil 就足够了。

The iPhone SDK UITableView optimizes UITableViewCell creation by reusing cells that aren't in view instead of recreating new ones all the time. That's done through your call of dequeueReusableCellWithIdentifier:.

In tableView:cellForRowAtIndexPath:, you need to ensure that the cell you dequeue gets cleared so it only contains new cell content. In this case, having a cell.imageView.image = nil before your if (r == 1) ... statement will suffice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文