【问题标题】:UITableView reloading data objective cUITableView重载数据目标c
【发布时间】:2013-12-26 04:08:52
【问题描述】:

我有一个带有自定义单元格的table view。每个自定义单元格都有scroll view,其中图像是按顺序添加的。图像尺寸相当大。除了图像之外,每个单元格中还存在一些其他数据。在重新加载表格视图之前,我将图像保存到db。问题是表格视图reloaddata 不断调用并且我在单元格内的滚动视图无法正常工作。在不影响内部滚动的情况下重新加载表格视图的有效方法是什么。图像也应该以正确的方式加载。

这就是我调用这些方法的方式:

-(void)viewWillAppear:(BOOL)animated{
 [self performSelectorInBackground:@selector(getPacksdetails) withObject:nil];}

-(void) getPacksdetails
{
    //NSAutoreleasePool *pool  = [[NSAutoreleasePool alloc] init];
    ForcePackRequest *request = [[ForcePackRequest alloc] init];
    User *userObj = (User *)[request getActiveUser];
    NSString *emailString =[NSString stringWithFormat:@"%@", userObj.Email ];

    BOOL isPackList= [request getPurchasedPacksOfUser:emailString toArray:packsListArray excerciseArray:excercisesArray recommendedGearsArray:recommendedGears];
    // NSMutableArray *namesArray=[[NSMutableArray alloc]init];

    for(int packscounter=0;packscounter<[packsListArray count];packscounter++){

        if([[[packsListArray objectAtIndex:packscounter]objectForKey:@"name"] isEqualToString: passedPackName]){
            //nslog(@"passedPackName%@",passedPackName);
            ////nslog(@"%@",data.exName);
            reservedIndexPath=nil;

            reservedIndexPath=   [NSIndexPath indexPathForRow:0 inSection:packscounter];
            //nslog(@"%@",reservedIndexPath);;
            break;
        }
        // reservedIndexPath=[NSIndexPath indexPathWithIndex:i];
    }

    if(isPackList)
        [self performSelectorInBackground:@selector(loadExerthumbThread:) withObject:nil];
}
- (void) loadExerthumbThread:(id)sender{
        //if (FP_DEBUG) //nslog(@"%@",excercisesArray);
        for(int i=0;i<[excercisesArray count];i++){
            NSMutableArray *exforAsection=[[NSMutableArray alloc]init];
            // exerciseArrayForeachSection=[excercisesArray objectAtIndex:i];

            exforAsection=[excercisesArray objectAtIndex:i];
            for (int intConter = 0; intConter <[exforAsection count]; intConter++)
            {
                Exercise *data = [exforAsection objectAtIndex:intConter];
                // NSString *imageName = [[data.exImage ] intValue];

                int intExerId = [data.exImage intValue];

                NSString *imagestr = [NSString stringWithFormat:@"hires_%d",intExerId];

                FileManager *objFileManager = [[FileManager alloc] init];
                NSData *imageData = nil;
                if ([objFileManager isFileExistsInDocDir:imagestr])
                {
                    imageData = [objFileManager getFileFormDocDirWithName:imagestr];
                }
                else
                {
                    NSString *imageUrlString = [NSString stringWithFormat:@"http://www.forcetherex.com/force_uploads/exercise/exercise_hires/%@.png",data.exId];
                    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageUrlString]];
                    if (data != nil)
                    {
                        if ([data length] > 100)
                            imageData = [data copy];
                    }
                    if (imageData != nil){
                        [objFileManager writeFileToAppDirectoryWithFileName:imagestr andFileData:imageData];                            
                        //Mark as dont back up
                        NSURL *fileUrl = [NSURL fileURLWithPath:imagestr];
                        [self addSkipBackupAttributeToItemAtURL:fileUrl];
                        fileUrl=nil;
                        data = nil;
                    }
                }
                if (imageData != nil)
                    data.exThumbImage = imageData;
                objFileManager = nil;
            }
            [self reloadTableView];
    }

而我的 cellforrow atindexpath 代码是:

{
 NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];

                //static NSString *CellIdentifier = @"CustomCellFor_Dashboard";
                _customCell = (CustomCellFor_Dashboard *)  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (_customCell == nil) {
                    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellFor_Dashboard" owner:self options:nil];
                    for (id currentObject in topLevelObjects){
                        if ([currentObject isKindOfClass:[UITableViewCell class]]){
                            _customCell =  (CustomCellFor_Dashboard *) currentObject;
                            _customCell.delegate=self;
                            break;
                        }
                    }
                }

                _customCell.exNameDictArray=[[packsListArray objectAtIndex:indexPath.section]objectForKey:@"exerciseList"];
                _customCell.indexPath=indexPath;
                NSLog(@"%d",sng.showingPath.section);
                if(indexPath.section ==sng.showingPath.section)
                    _customCell.exnameTable.hidden=FALSE;

                sectionInt=indexPath.section;

                exerciseArrayForeachSection=[[NSMutableArray alloc]init];

                //[exerciseArrayForeachSection removeAllObjects];
                exerciseArrayForeachSection=[excercisesArray objectAtIndex:indexPath.section];

                //next btn
                UIButton *accessoryView = [[UIButton alloc] initWithFrame: _customCell.nextBtn.frame];
                accessoryView.tag = indexPath.section;
                [accessoryView setImage:[imageasArr objectAtIndex:0]forState:UIControlStateNormal];

                [accessoryView addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [_customCell addSubview:accessoryView];
                //_customCell.accessoryView = accessoryView;

                    //prev btn
                    UIButton *prevBtn = [[UIButton alloc] initWithFrame: _customCell.prevBtn.frame];
                    prevBtn.tag = indexPath.section;
                    [prevBtn setImage:[imageasArr objectAtIndex:2]forState:UIControlStateNormal];

                    [prevBtn addTarget:self action:@selector(previousButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                    [_customCell addSubview:prevBtn];

                _customCell.selectionStyle = UITableViewCellSelectionStyleNone;

                [_customCell addSubview:_customCell.buttonView];
                _customCell.nameLabel.text=[[packsListArray objectAtIndex:indexPath.section]objectForKey:@"name"];

                _customCell.exCountLbl.text=[NSString stringWithFormat:@"%@",   [[packsListArray objectAtIndex:indexPath.section]objectForKey:@"exercises"]];

                _customCell.scroll.delegate = self;

                //  [_customCell.scroll setBackgroundColor:[UIColor blackColor]];
                [_customCell.scroll setCanCancelContentTouches:NO];

                _customCell.scroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
                _customCell.scroll.clipsToBounds = YES;
                [_customCell.scroll setContentOffset:CGPointMake(0, 20)];

                if([exerciseArrayForeachSection count]>0){
                    _customCell. scroll.frame = CGRectMake(0,40, 320, _customCell.scroll.frame.size.height-10);

                    _customCell. scroll.contentSize = CGSizeMake(320*[exerciseArrayForeachSection count],_customCell .scroll.frame.size.height);
                    int cx = 30;
                    for(int i=0;i<[exerciseArrayForeachSection count];i++){
                        Exercise *data = [exerciseArrayForeachSection objectAtIndex:i];
                        UIView *detailView=[[UIView alloc]initWithFrame:_customCell.excerciseDetailsView.frame];

                        UILabel *titleLbl=[[UILabel alloc]initWithFrame:_customCell.exTitleLabel.frame];
                        titleLbl.font=[UIFont systemFontOfSize:12];

                        UIImageView *exerciseImg=[[UIImageView alloc]initWithFrame: _customCell.exThumbImageView.frame];


                        UIButton *playBtn=[[UIButton alloc]initWithFrame:_customCell.exThumbButton.frame];
                        playBtn.showsTouchWhenHighlighted=YES;

                        [playBtn setImage:[imageasArr objectAtIndex:1]forState:UIControlStateNormal];

                        // playBtn.frame =_customCell.exThumbButton.frame;
                        playBtn.tag=i;

                        [playBtn addTarget:self action:@selector(videoPlayActn:) forControlEvents:UIControlEventTouchUpInside];

                        [titleLbl setText:data.exName];

                        if ([data.exThumbImage length] > 0)
                            [exerciseImg setImage:[UIImage imageWithData:data.exThumbImage]];


                        [detailView addSubview:titleLbl];

                        [exerciseImg addSubview: playBtn];
                        [detailView addSubview:exerciseImg];
                        //  _customCell.exThumbButton= playBtn;
                        [exerciseImg bringSubviewToFront:playBtn];
                        exerciseImg.userInteractionEnabled=TRUE;
                        [detailView bringSubviewToFront:playBtn];
                        //if (FP_DEBUG)   //nslog(@"%f",_customCell.scroll.frame.origin.x);
                        detailView.frame=CGRectMake(cx, 30, 320, _customCell.scroll.contentSize.height);
                        //_customCell.exThumbImageView=exerciseImg;
                        [_customCell.scroll addSubview:detailView];
                        detailView=nil;
                        exerciseImg=nil;
                        titleLbl=nil;
                        _customCell.scroll.contentSize = CGSizeMake( cx,_customCell.scroll.contentSize.height);
                        cx = cx+_customCell.scroll.frame.size.width;

                        //if (FP_DEBUG) //nslog(@"%i",cx);
                    }
                }
                //  [_customCell.scroll setContentOffset:CGPointMake(0, 20)];

                //if (FP_DEBUG) //nslog(@"%f",_customCell.scroll.contentOffset.x);

                [_customCell.howyoulfeelBtn  addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];

                [_customCell.expertAdviceBtn  addTarget:self action:@selector(expertbuttonclicked:) forControlEvents:UIControlEventTouchUpInside];

                [_customCell.recoverTrackerBtn  addTarget:self action:@selector(recoverytrackerBtnclicked:) forControlEvents:UIControlEventTouchUpInside];

                [_customCell.recommendedGearsBtn  addTarget:self action:@selector(recommendedGearsBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

                ////nslog(@"ewfewr************ewr");

                if(indexPath.section== expandViewclickedSection&&_isexpanded==TRUE){

                    _customCell.scroll.frame = CGRectMake(-160, 22, self.view.frame.size.width, self.view.frame.size.height);
                    _customCell.shareView.hidden=FALSE;
                    //                _customCell.nextBtn.hidden=TRUE;
                    accessoryView.hidden=TRUE;
                    //_isexpanded=TRUE;
                }
                else  if(indexPath.section== expandViewCollapsedSection&&_isexpanded==FALSE) {

                    _customCell.scroll.frame = CGRectMake(-0, 22, self.view.frame.size.width, self.view.frame.size.height);
                    _customCell.shareView.hidden=TRUE;
                    //  _isexpanded=FALSE;
                }
                if(_isExListingTablePresented==TRUE&&indexPath.section==exlistTableAddedSection){

                    _customCell.exListingTable.hidden=FALSE;
                                 }
                else if(_isExListingTablePresented==FALSE&&indexPath.section==_exlistTableremovedSection){
                    _customCell.exListingTable.hidden=TRUE;
                                  }                    
                return _customCell;
}

【问题讨论】:

标签: ios objective-c uitableview custom-controls


【解决方案1】:

首先,将所有与 DB/IO 调用相关的代码转移到另一个控制器,并对其进行异步调用,并通过委托方法(或您认为合适的任何其他方法)返回结果。这将提高代码的整体性能,并且滚动视图可以正常工作。

如果这不能让事情变得完美,请将 DB/IO 调用放在一个新线程中,这将确保您的滚动视图(以及您的整个应用)运行良好。

【讨论】:

    【解决方案2】:
    1. 您应该在主线程中更新 UI。

      [self performSelectorOnMainThread:reloadTableView withObject:nil waitUntilDone:NO];

    2. 您不应该以如此高的频率重新加载表格,如果单元格中的内容过多,这会降低您的应用程序的速度。在所有数据更新后尝试重新加载表。当然,数据更新应该在后台线程中运行。

    3. 我看到你用行号和节号设置单元格标识符,所以你可以用它做一些技巧。就像比较图片 url 来决定是否 setImage(这会消耗很多性能)。

    【讨论】:

    • 我尝试在加载所有数据后重新加载表格。但是表格视图上出现单元格需要很长时间。
    • 我猜,你不会在 heightForRowAtIndexPath 中做 cellForRowAtIndexPath 吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多