簡體   English   中英

UITableView單元格正方形而不是圓形

[英]UITableView cell square instead of round

我有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時,圖像為什么會正方形?

我認為問題與此有關:

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

希望能幫助到你!

由於您獲取的圖像會調整為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