UITableView单元格而不是圆形

时间:2014-07-29 13:31:11

标签: ios objective-c uitableview

我有UICollectionView,当我点击收集单元格UITableView时出现。我想要 UITableViewCell的图像是圆的,当我第一次加载这个表时,所有图像都是圆的,当我选择并移动内部细节控制器,然后切换回它们仍然是圆的。但是,当我进入UITableView(从UICollectionView下来),然后按“返回”按钮再次看到UICollectionView,然后按UICollectionView单元格,它显示UITableView与可见的“方形(默认)图像”单元格。为什么我说可见?好吧,当我向下滚动时,新细胞将再次呈圆形。为什么会出现这个讨厌的错误?有我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    myCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
    labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"];

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

    [cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageFirst"]] placeholderImage:[UIImage imageNamed:@"noPhoto.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){

        UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        cell.imageView.image = resizedImage;
        cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.width /2.0f;
        cell.imageView.clipsToBounds = YES;
        cell.separatorInset = UIEdgeInsetsMake(0, 82, 0, 0);
    }];
    self.stringToPass    = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"description"];

    return cell;
}

当用户返回第一个视图然后再次切换到UITableView时,为什么图像会变成方块?

2 个答案:

答案 0 :(得分:3)

我认为问题与此有关:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

尝试在单元格内设置图像(您使用的是SDWebImage)吗?

// Set the corner radius and clip to bounds before setting the image
[cell.imageView.layer setCornerRadius:cell.imageView.frame.size.width/2];
cell.imageView.clipToBounds = YES;
// I'm using SDWebImage
[cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageFirst"]] placeholderImage:[UIImage imageNamed:@"noPhoto.png"]];

希望它有所帮助!

答案 1 :(得分:1)

由于您获取的图像已调整为66x66的明确尺寸,因此无论正在设置的图像如何,都可以设置cornerRadius

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    …
    cell.imageView.layer.cornerRadius = 33.0;
    cell.imageView.layer.masksToBounds = YES;
    …
    return cell;
}