我有一个包含四种不同类型细胞的tableview。在每个单元格上我需要放置一个图像和标签显示不同类型的文本和标签,所以为了更好的性能我做的是在 scrollViewDidEndDecelerating 我试图只在可见单元格上渲染图像。当我使用 reloadRowsAtIndexPaths 重新加载特定的单元格时,它正在崩溃。将错误消息显示为 [YesNoCell compare:]:无法识别的选择器发送到实例0x1edc6a40 。任何想法都是我错了..
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Feed *feedObject = [self.feedList objectAtIndex:indexPath.row];
BOOL isTableDecelarating = NO;
if([tableView isDecelerating]){
isTableDecelarating = YES;
}
switch ([feedObject.votingStyle intValue]) {
case 4:{
StarCell *cell =(StarCell *)[tableView dequeueReusableCellWithIdentifier:@"starCell"];
if(!cell){
NSLog(@"StarCell");
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StarCell" owner:self options:nil];
cell = (StarCell *)[nib objectAtIndex:0];
[cell setBackgroundColor:[UIColor clearColor]];
[cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];
cell.avatarButton.layer.borderWidth = 2.0f;
}
cell.feedBgImage.image = nil;
if([feedObject.feedType intValue] == 3){
if(!isTableDecelarating)
cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];
}
else if([feedObject.feedType intValue] ==2){
cell.videoButton.hidden = NO;
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
}
}
else{
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
}
}
return cell;
break;
}
case 2:{
YesNoCell *cell =(YesNoCell *)[tableView dequeueReusableCellWithIdentifier:@"yesNoCell"];
if(!cell){
NSLog(@"YesNoCell");
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YesNoCell" owner:self options:nil];
cell = (YesNoCell *)[nib objectAtIndex:0];
[cell setBackgroundColor:[UIColor clearColor]];
[cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];
cell.avatarImage.layer.borderWidth = 2.0f;
}
cell.feedBgImage.image = nil;
if([feedObject.feedType intValue] == 3){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];
}
else if([feedObject.feedType intValue] ==2){
cell.videoButton.hidden = NO;
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
}
}
else{
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
}
}
return cell;
break;
}
case 3:{
YNMayBeCell *cell =(YNMayBeCell *)[tableView dequeueReusableCellWithIdentifier:@"ynmCell"];
if(!cell){
NSLog(@"YNMayBeCell");
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YNMayBeCell" owner:self options:nil];
cell = (YNMayBeCell *)[nib objectAtIndex:0];
[cell setBackgroundColor:[UIColor clearColor]];
[cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];
cell.avatarButton.layer.borderWidth = 2.0f;
}
cell.feedBgImage.image = nil;
if([feedObject.feedType intValue] == 3){
if(!isTableDecelarating)
cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];
}
else if([feedObject.feedType intValue] ==2){
cell.videoButton.hidden = NO;
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
}
}
else{
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
}
}
return cell;
break;
}
default:{
ThumbsUpCell *cell =(ThumbsUpCell *)[tableView dequeueReusableCellWithIdentifier:@"thumbsUpCell"];
if(!cell){
NSLog(@"ThumbsUpCell");
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ThumbsUpCell" owner:self options:nil];
cell = (ThumbsUpCell *)[nib objectAtIndex:0];
[cell setBackgroundColor:[UIColor clearColor]];
[cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];
cell.avatarButton.layer.borderWidth = 2.0f;
}
cell.feedBgImage.image = nil;
[cell.loadIndicator startAnimating];
[cell.loadIndicator setHidesWhenStopped:YES];
if([feedObject.feedType intValue] == 3){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];
}
else if([feedObject.feedType intValue] ==2){
cell.videoButton.hidden = NO;
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
}
}
else{
UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];
if(image){
[cell.loadIndicator stopAnimating];
if(!isTableDecelarating)
cell.feedBgImage.image =image;
}
else{
[self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
}
}
return cell;
break;
}
}
}
滚动型:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView
{
NSArray *visibleRows = [self.homeTableView visibleCells];
[self.homeTableView beginUpdates];
[self.homeTableView reloadRowsAtIndexPaths:visibleRows withRowAnimation:UITableViewRowAnimationNone];
[self.homeTableView endUpdates];
}
答案 0 :(得分:4)
那是因为reloadRowsAtIndexPaths:需要一个indexPaths数组,但是你提供了一个表视图单元格对象的列表。
只需更改行
即可 NSArray *visibleRows = [self.homeTableView visibleCells];
进入
NSArray *visibleRows = [self.homeTableView indexPathsForVisibleRows];
答案 1 :(得分:0)
我被困在这个问题上一天半 这是我的代码。
-(void)ShowtableHeight:(NSIndexPath*)indexpath
{
NSArray *visibleRows = [recTableView indexPathsForVisibleRows];
[recTableView reloadRowsAtIndexPaths:visibleRows withRowAnimation:UITableViewRowAnimationNone];
}
我从
调用了这个函数- (void)webViewDidFinishLoad:(UIWebView *)aWebView
我的tableView Rows在webView中显示数据。
因此,对于数据的动态高度,我编写了ShowtableHeight函数。并且ShowtableHeight函数的indexpath参数来自webViewDidFinishLoad函数,如下所示,
NSIndexPath *index = [recTableView indexPathForCell:cell];
如果您想将section作为参数传递,那么您可以在webViewDidFinishLoad中相应地编写以下内容
NSString *key = [dateArr objectAtIndex:index.section];
这将传递tableview中的部分数量。