滚动自定义 UITableView 单元格的 TableView 时出现一些奇怪的行为。
应用首次打开时,内容正常:
但是如果我滚动我的 View ,一些偏移量会出现在嵌入式 UIImageViews 中:
我已经删除了所有布局约束,但问题仍然存在。
我不知道为什么会发生这种情况;欢迎任何帮助!
这是我的 cellForRowAtIndexPath
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *StrechCellIdentifier = @"StrechCell";
//Place regular strechable cells.
GFStrechCell *cell = [tableView dequeueReusableCellWithIdentifier:StrechCellIdentifier];
if (cell == nil) {
cell = (GFStrechCell*) [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:StrechCellIdentifier];
}
cell.nameLabel.text = @"Chez Julien";
cell.descriptionLabel.text = @"A yummy place!";
cell.descriptionLabel.hidden = YES;
[cell.restaurantImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"restaurant%i.png", (indexPath.row%5 + 1)]]];
if(indexPath.row % 2 == 0){
[cell.favoriteButton.imageView setImage:[UIImage imageNamed:@"favoriteButtonPressed.png"]];
} else {
[cell.favoriteButton.imageView setImage:[UIImage imageNamed:@"favoriteButton.png"]];
}
return cell;
}
GFStrechCell
只是 call 的一个简单子(monad)类,其元素指向 Storyboard 中的 IBOutlets:
最佳答案
这很奇怪,试试看它是否有效
下面:
[cell.restaurantImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"restaurant%i.png", (indexPath.row%5 + 1)]]];
尝试添加:
CGRect restaurantImageFrame = cell.restaurantImage.frame;
restaurantImageFrame.origin = CGPointZero; //or whatever it should be
cell.restaurantImage.frame = restaurantImageFrame;
这会将图像的原点设置为 (0,0)。
编辑:您也可以尝试添加以下行。
[cell.restaurantImage sizeToFit];
关于ios - 滚动 UITableView 时图像内容出现偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230557/