[英]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.