简体   繁体   English

UITableView单元格正方形而不是圆形

[英]UITableView cell square instead of round

I have UICollectionView, when i tap collection cell UITableView appear. 我有UICollectionView,当我点击收集单元格UITableView时出现。 I want that UITableViewCell's image be round, and when i first load this table all images are round, when i select in and move inside detail controller, then switch back they are still round. 我希望UITableViewCell的图像是圆形的,当我第一次加载该表时,所有图像都是圆形的,当我选择并移入细节控制器内部时,再切换回它们仍然是圆形的。 But, when i enter UITableView (down from UICollectionView), then press "Back" button to see UICollectionView again, then press UICollectionView cells, it shows me UITableView with visible "square (default) image" cell. 但是,当我输入UITableView(从UICollectionView向下)时,然后按“返回”按钮再次查看UICollectionView,然后按UICollectionView单元格,它向我显示带有可见的“方形(默认)图像”单元格的UITableView。 Why i say visible? 为什么我说可见? Well, when i scroll down, new cells are round again as they should be. 好吧,当我向下滚动时,新的单元格将再次恢复原状。 Why this nasty bug appear? 为什么会出现这个讨厌的错误? There is my code: 有我的代码:

- (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;
}

Why images square when user get back to first view and then again switch to UITableView? 当用户返回第一个视图然后再次切换到UITableView时,图像为什么会正方形?

I think that the problem is related with this: 我认为问题与此有关:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

Try that for setting the image inside the cell (are you using SDWebImage)? 尝试在单元格内设置图像(您是否正在使用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"]];

Hope it helps! 希望能帮助到你!

Since the images you fetch resized to definite dimensions of 66x66, you can set the cornerRadius irrespective of the image being set. 由于您获取的图像会调整为66x66的确定尺寸,因此无论设置的图像如何,都可以设置cornerRadius

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM