我的問題是,並非所有單元格都正確使用正確的UIView作爲渲染圖像進行初始化。(請參閱下面的代碼)在iPhone 4上,它始終與iPhone上的視網膜顯示器同一個cel也是其他細胞。在這個設置中,setNeedsDisplay將不起作用。 如果我在IBoutlet中使用相同的結構,它就可以工作。 ! 我需要在一些單元格中使用圖像文件.png和其他一些單元格定義的繪圖方法,它使用drawrect或更好的我應該使用setNeedsDisplay方法.....SetNeedsDisplay在表格視圖
什麼是錯了! !
我的代碼
的tableview ...細胞
static NSString *CellIdentifier = @"TypeCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
CellTypes *cellType = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = cellType.title;
cell.detailTextLabel.text = cellType.detail;
if ([cellType.type rangeOfString:@"TL"].location != NSNotFound) {
cell.imageView.image = [UIImage imageNamed:[cellType.type stringByAppendingString:@"Thumb"]];
cell.indentationWidth = 10;
}
else {
static CGFloat scale = 0.0; // old API , screen values
UIScreen *screen = [UIScreen mainScreen];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
scale = [screen scale];
}
if (scale>0.0) {
UIGraphicsBeginImageContextWithOptions(cell.imageView.bounds.size, NO, scale);
}
else {
UIGraphicsBeginImageContext(cell.imageView.bounds.size);
}
CGRect imageRect = CGRectMake(0, 0, 84, 84); // cell.imageView.bounds;
FittingImageView *fittingImage = [[FittingImageView alloc] initWithFrame:imageRect];
fittingImage.thumb = YES;
fittingImage.title = offSetType.title;
[fittingImage drawRect:imageRect]; // this works but skips one imageRect
// [fittingImage setNeedsDisplay]; //this won't work.
[cell.layer renderInContext:UIGraphicsGetCurrentContext()];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return cell;
此外,如果FittingImageView就像是一個UIImageView任何事情,你只應在創建單元創建這些 - 你不需要添加一個'時給dequeueReusableCellWithIdentifier'你指已經有它。你確定你不想'[cell addSubview:fittingImage];'? – 2013-02-19 23:16:17
嗨,fittingImage是一個UIView類,它繪製幾行直線圈出,中風和填充模式。我知道我不應該使用直角,並且正在選擇正確的方式來繪製物品。但是fittingImage取決於每個單元的值。我應該使用比[cell addSubView:fittingImgae]; – 2013-02-20 16:21:49
正確。傳遞適合圖像需要的任何數據,並在請求時讓它自行繪製。 – 2013-02-21 03:01:40