[英]Reusable UITableviewcell not displaying initial images correctly
所以我有一個帶有動態原型的UITableview。 單元格是具有4個屬性的自定義單元格:
我在原始的情節提要中可以正常工作,但是最近又轉到了新的情節提要。 (僅供參考,我無法使用舊的情節提要板進行重新測試,因為對代碼進行了一些更改以排除這種情況)。
**這是一個自動布局的問題。 禁用自動布局可解決問題(但原因是自動布局)。 **
基本上,問題是這樣的:
**編輯**
下面的一些示例代碼...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PersonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"personCell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personCell"];
}
cell.delegate = self;
cell.dataSource = self;
cell.backgroundColor = _blueColor;
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
-(void)configureCell:(PersonCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Person *person = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.imageVBkg.image = [[UIImage imageNamed:@"list-item-background"] resizableImageWithCapInsets:UIEdgeInsetsMake(50, 50, 30, 30)];
cell.mainText.text = [NSString stringWithFormat:@"%@ %@", person.firstname, person.lastname];
Company *company = (Company *)person.worksFor;
NSString *fullString = person.position;
if ([company.name length] > 0 && [person.position length] > 0) {
fullString = [fullString stringByAppendingString:@" at "];
}
if (company.name != nil) {
fullString = [fullString stringByAppendingString:company.name];
}
cell.subtitle.text = fullString;
if ([person hasPhoto]) {
_image = [person photoImage];
if (_image != nil) {
cell.personImageView.clipsToBounds = YES;
cell.personImageView.image = _image;
cell.personImageView.layer.cornerRadius = cell.personImageView.bounds.size.width / 2.0f;
}
}
}
PersonCell.h具有以下屬性:
@property (nonatomic, weak) IBOutlet UILabel *mainText;
@property (nonatomic, weak) IBOutlet UILabel *subtitle;
@property (nonatomic, weak) IBOutlet UIImageView *personImageView;
@property (strong, nonatomic) IBOutlet UIImageView *imageVBkg;
還值得一提的是...如果我不使用“ cell.personImageView.clipsToBounds = YES;” 然后出現personImageView圖像,但顯示為正方形而不是圓形,這就是我需要的。
希望這是一個簡單的解決方法...
在此先感謝您的任何建議。
在這里的單元格中使用此代碼行122,111是表格視圖單元格中用於圖像視圖的幀
CGSize itemSize = CGSizeMake(122,111);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
首先,您需要標記您的單元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PersonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"personCell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personCell"];
}
cell.delegate = self;
cell.dataSource = self;
cell.backgroundColor = _blueColor;
//See these two lines
//Edit
cell.tag = indexPath.row;
[self configureCell:cell];
return cell;
}
第二,您必須像這樣更改您的配置單元格方法。
-(void)configureCell:(PersonCell *)cell
{
//Edit
NSInteger index = (NSInteger) (cell.tag)
NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
// after this place your code.
}
出於某種原因,完全禁用自動布局和大小類,然后重新啟用並重新配置它們均已解決。 僅僅消除約束是行不通的。 不知道真正的問題是什么。 歡迎提出建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.