2

我有一个包含四种不同类型单元格的表格视图。在每个单元格上,我需要放置一个显示不同类型文本和标签的图像和标签,因此为了获得更好的性能,我所做的是在scrollViewDidEndDecelerating上,我试图仅在可见单元格上渲染图像。当我使用reloadRowsAtIndexPaths重新加载特定单元格时,它崩溃了。将错误消息显示为[YesNoCell compare:]: unrecognized selector sent to instance 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];

}
4

2 回答 2

4

这是因为 reloadRowsAtIndexPaths: 需要一个 indexPaths 数组,但您提供的是表格视图单元格对象的列表。

换行就好

  NSArray *visibleRows = [self.homeTableView visibleCells];

进入

  NSArray *visibleRows = [self.homeTableView indexPathsForVisibleRows];
于 2013-10-03T13:31:52.860 回答
0

我被困在这个问题上 1 天半。
这是我的代码。

-(void)ShowtableHeight:(NSIndexPath*)indexpath
{
    NSArray *visibleRows = [recTableView indexPathsForVisibleRows];
    [recTableView reloadRowsAtIndexPaths:visibleRows  withRowAnimation:UITableViewRowAnimationNone];
} 

我从

- (void)webViewDidFinishLoad:(UIWebView *)aWebView 

我的 tableView 行在 webView 中显示数据。

因此,对于数据的动态高度,我编写了 ShowtableHeight 函数。ShowtableHeight 函数的 indexpath 参数来自 webViewDidFinishLoad 函数,如下所示,

NSIndexPath *index = [recTableView indexPathForCell:cell];

如果您想将部分作为参数传递,那么您可以在 webViewDidFinishLoad 中相应地编写以下内容

NSString *key = [dateArr objectAtIndex:index.section];

这将传递 tableview 中的部分数。

于 2014-06-30T11:08:43.117 回答