0

我在将图像从网络服务器加载到表格视图中的 UIImageView 时遇到问题

这段代码完美运行,但图像在我的 tableview 中被放置得很奇怪和丑陋:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the cell. Note that this name is the same as in the storyboard
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Set the correct name in the cell.
    //Do so by looking up the row in indexpath and choosing the same element in the array
    NSInteger currentRow = indexPath.row;
    Image* currentImage = [self.images objectAtIndex:currentRow];

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:@"testimage.jpg"]];
    return cell;
}

所以我在情节提要上创建了一个 UIImageView,并将代码更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the cell. Note that this name is the same as in the storyboard
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Set the correct name in the cell.
    //Do so by looking up the row in indexpath and choosing the same element in the array
    NSInteger currentRow = indexPath.row;
    Image* currentImage = [self.images objectAtIndex:currentRow];

    UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];
    // Here we use the new provided setImageWithURL: method to load the web image
    [imgView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:@"testimage.jpg"]];
    return cell;
}

但是,如果我在我的设备上运行此代码,我会收到信号 SIGABRT 错误。

我怎样才能让它在我的自定义 Imageview 上工作?

4

2 回答 2

0

您获得 SIGABRT 是因为您的 imagevIview 未添加到单元格,而是单元格的contentView..!所以获取 imageView 的正确方法应该是,

UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];
于 2013-10-31T10:00:20.777 回答
0

In the first code snippet, add these lines:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
于 2013-10-31T09:13:05.170 回答