がUITableViewCell
あり、それらのいくつかにインタースティシャル画像をランダムに配置する必要があります。これは機能していますが、セルのキャッシュにより、画像はテーブルのさらに下に繰り返されています。セル 1、12、および 25 に画像を配置することを事前に決定したとします。これらのセルには画像が含まれますが、セル 1 の後、セル 4、8 などにも最初の画像が含まれる可能性があります。
cellForRowAtIndexPath
画像を追加する基準を満たしていない場合は、キャッシュをクリアして画像を削除するように具体的に指示する必要があると思います。どうすればそれを行うことができますか?私はすでにelse
、決定を下して画像を割り当てる if ステートメントを試しました。その で、の画像をにelse
設定しようとしましたが、違いはありませんでした。UIImageView
nil
これらの画像が定期的にページを下ってスパムするのを防ぐために、キャッシュをクリアする他の方法はありますか? これが私のcellForRowAtIndexPath
方法であり、現在その最後の行にある行else
は画像に影響を与えていないため、機能するものに置き換える必要があると思います!
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
// Make and allocate the cell if necessary.
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Cell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"Cell"];
}
cell.lineLabel.text = [[self.fetchedResultsController objectAtIndexPath: indexPath] line];
cell.actorLabel.text = [[self.fetchedResultsController objectAtIndexPath: indexPath] actor];
[cell.lineLabel sizeToFitMultipleLines];
// Get the size each label needs to be when constrained to set width and very long height (8000).
CGSize labelSize = [cell.lineLabel.text sizeWithFont: cell.lineLabel.font constrainedToSize: CGSizeMake(265, 8000)];
if ([[cellsToContainInterstitialImages allKeys] containsObject: [thisLine.lineID stringValue]]) {
UIImageView *interstitialImage = [[UIImageView alloc] init];
NSNumber *cellTypeNumber = [cellsToContainInterstitialImages valueForKey: [thisLine.lineID stringValue]];
kInterstitialCellType cellType = [cellTypeNumber intValue];
if (cellType == kInterstitialCellTypeFirst) {
interstitialImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"man.png"]];
}
else if (cellType == kInterstitialCellTypeSecond) {
interstitialImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"woman.png"]];
}
else {
interstitialImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"child.png"]];
}
[interstitialImage setFrame: CGRectMake(10, cell.lineLabel.frame.size.height + cell.actorLabel.frame.size.height + 10, 150, 150)];
[cell addSubview: interstitialImage];
} else {
UIImageView *interstitialImage = [[UIImageView alloc] init];
[interstitialImage setImage: nil];
}
return cell;
}