【问题标题】:UITableView cell square instead of roundUITableView 单元格方形而不是圆形
【发布时间】:2014-07-29 13:31:11
【问题描述】:

我有 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 时,为什么图像是方形的?

【问题讨论】:

  • cell.imageView.clipsToBounds 替换为cell.imageView.layer.masksToBounds
  • 它不起作用,甚至警告我“黄色”错误..
  • 错误说明了什么?
  • 对不起,我错了。没有黄色错误,但有相同的“错误”。首先它是四舍五入的,当我切换到主屏幕并再次进入 tableView 时,它是正方形的。
  • 将图层舍入代码放在提交给 SDWebImage 的块之外,圆角半径为 33,因为您知道图像的尺寸。

标签: ios objective-c uitableview


【解决方案1】:

我认为问题与此有关:

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"]];

希望对你有帮助!

【讨论】:

  • 它确实使它变圆了,但错误并没有消失.. :(
  • 如果圆了,有什么bug?
  • 当我切换回主屏幕时,然后点击 UICollectionView 以查看包含 UITableView 的第二个 View 控制器,这一次图像是平方的。
【解决方案2】:

由于您获取的图像已调整为 66x66 的确定尺寸,因此您可以设置 cornerRadius 而与设置的图像无关。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2018-07-13
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多