当我向下滚动 UITableView 以加载更多结果时,我正在使用旋转活动指示器来显示进度。它应该像这样工作:如果我到达数据数组的最后一个索引,UITableView 将在下一个单元格上显示一个活动指示器。但是,每当我摆脱旧数据数组并使用新数据数组并使用reloadData
活动指示器时,它都不会消失。活动指示器将位于另一个现有数据单元格的同一单元格中。在调试窗口中,它会显示一些 CGContext 错误:
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextSetRGBFillColor: invalid context 0x0
<Error>: CGContextFillRects: invalid context 0x0
<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextSetFont: invalid context 0x0
<Error>: CGContextSetTextMatrix: invalid context 0x0
<Error>: CGContextSetFontSize: invalid context 0x0
<Error>: CGContextSetTextPosition: invalid context 0x0
<Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0
我该如何解决这个问题?
这是我在 cellForRowAtIndexPath 的代码:
TNSTableViewCell *videoCell = [tableView dequeueReusableCellWithIdentifier:@"videoCell"];
if (videoCell == nil) {
videoCell = [[TNSTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"videoCell"];
}
// When scroll down last index of the index load next page
if (indexPath.row >= self.entries.count) {
if (!loading) {
NSLog(@"Loading new records...");
loading = YES;
if (fetchBatch != 0) {
[informationDownloader getSearchRecordsWithKeywords:nil];
}
}
}
if (indexPath.row < self.entries.count) {
if (videoCell.activityIndicator) {
videoCell.activityIndicator = nil;
}
TNSVideoRecord *record = [self.entries objectAtIndex:[indexPath row]];
videoCell.textLabel.text = record.title;
videoCell.textLabel.backgroundColor = [UIColor clearColor];
videoCell.detailTextLabel.numberOfLines = 2;
videoCell.detailTextLabel.text = [@"views: " stringByAppendingFormat:@"%@\n%@", [record.viewCount stringValue], record.uploader];
videoCell.detailTextLabel.backgroundColor = [UIColor clearColor];
if (!record.image) {
if (self.thumbnailVideoTableView.dragging == NO && self.thumbnailVideoTableView.decelerating == NO) {
[informationDownloader downloadThumbnailForRecord:record];
}
// if a download is deferred or in progress, return a placeholder image
UIImageView *imageView = videoCell.imageView;
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.alpha = 1.0;
indicator.center = imageView.center;
[imageView addSubview:indicator];
[indicator startAnimating];
} else {
videoCell.imageView.image = record.image;
}
videoCell.imageView.userInteractionEnabled = YES;
videoCell.imageView.tag = [indexPath row];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playVideo:)];
[videoCell.imageView addGestureRecognizer:tapped];
UIImage *playButtonImage = [UIImage imageNamed:@"playbutton.png"];
NSString *durationText = [TNSUtilities convertDuration:record.length];
UIImage *image = [TNSUtilities drawText:durationText inImage:[TNSUtilities drawImage:playButtonImage inImage:videoCell.imageView.image]];
videoCell.imageView.image = image;
videoCell.accessoryView.tag = [indexPath row];
videoCell.accessoryView = [self makeAddButton];
videoCell.accessoryView.userInteractionEnabled = YES;
[videoCell setNeedsLayout];
} else {
NSLog(@"Displaying spinning indicator.");
videoCell.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
videoCell.activityIndicator.backgroundColor = [UIColor clearColor];
[videoCell addSubview:videoCell.activityIndicator];
videoCell.activityIndicator.frame = CGRectMake(self.view.frame.size.width/2 - 15, videoCell.frame.size.height/2 - 15, 30, 30);
videoCell.backgroundColor = [UIColor clearColor];
[videoCell.activityIndicator startAnimating];
[videoCell setNeedsLayout];
}
return videoCell;
如您所见,imageView 中还有另一个活动指示器,并且在我更新数据数组后,该指示器也保留在 imageView 上。奇怪的是,该指标并未出现在每个 uitableviewcell 上。
这是我在 TNSTableViewCell 中的 prepareReuse 实现,它是 UITableViewCell 的子类:
- (void)prepareForReuse
{
if (self.activityIndicator) {
[self.activityIndicator stopAnimating];
self.activityIndicator = nil;
}
self.textLabel.text = nil;
self.detailTextLabel.text = nil;
}
这里是drawText方法和drawImage方法:
+ (UIImage *)drawText:(NSString *)text inImage:(UIImage *)image
{
UIGraphicsBeginImageContext(image.size);
CGRect bgRect = CGRectMake( 0, 0, image.size.width, image.size.height);
[image drawInRect: bgRect];
CGFloat rectWidth;
if (text.length < 6) {
rectWidth = 40.0f;
} else {
rectWidth = 56.0f;
}
CGRect textRect = CGRectOffset(CGRectMake(0, 0, rectWidth, 14), bgRect.size.width - rectWidth, bgRect.size.height - 14);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f);
CGContextFillRect(context, textRect);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
[text drawInRect:textRect withFont:[UIFont systemFontOfSize:14] lineBreakMode:NSLineBreakByClipping];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+ (UIImage *)drawImage:(UIImage *)fgImage inImage:(UIImage *)bgImage
{
UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0);
CGRect bgRect = CGRectMake( 0, 0, bgImage.size.width, bgImage.size.height);
[bgImage drawInRect:bgRect];
[fgImage drawInRect:CGRectOffset(CGRectMake(0, 0, fgImage.size.width, fgImage.size.height), bgRect.size.width/2 - fgImage.size.width/2, bgRect.size.height/2 - fgImage.size.height/2)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}