UITableView单元格奇怪地消失了 [英] UITableView cells strangely disappearing

查看:31
本文介绍了UITableView单元格奇怪地消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Marko团队来找你

coming to you from team Marko

我们正在处理一个令人难以置信的奇怪问题,即我们的表格视图单元格通常如下所示:

We are havign an incredible strange problem in which our table view cells, which normally appear like so:

显示如下:

经审查,当您将iPhone置于睡眠状态,然后重新打开应用程序并转到放置了tableview的uiviewcontroller时,似乎发生了该错误.这样就可以做到,即使委托和数据源仍设置为self,也不再调用cellForRowAtIndexPath.我们的节和行号仍然是正常的,我们可以在表格视图上向下滚动.但是由于未调用cellForRowAtIndexPath,因此隐藏了tableviewcell.

Upon review, the bug seems to happen when you put the iPhone to sleep, then reopen the app and going to the uiviewcontroller in which the tableview is placed. Somehow this makes it so that cellForRowAtIndexPath no longer gets called, even though the delegate and dataources are still set to self. Our sections and row numbers are still normal and we are able to scroll down on the tableview. But since cellForRowAtIndexPath isn't getting called, the tableviewcells are hidden.

这是我们的tableview设置(我从这个大文件中删除了与tableview无关的内容:

Here is our tableview setup (I took out stuff from this huge file that has nothing to do with the tableview:

//
//  SPHomeViewController.m
//  Spek

@interface SPHomeViewController () <UITableViewDataSource, UITableViewDelegate, MKMapViewDelegate, SPCreationViewDelegate, UIAlertViewDelegate, CLLocationManagerDelegate>
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSMutableArray* tableDatasource;
@property (nonatomic, strong) NSMutableArray* datasource;
@property (nonatomic, strong) NSMutableArray* friendsDatasource;
@property (nonatomic, strong) UISegmentedControl* userFilterSegment;
@property (nonatomic) BOOL isLoadingData;
@end

@implementation SPHomeViewController

@synthesize datasource = _datasource;
@synthesize friendsDatasource = _friendsDatasource;
@synthesize tableDatasource = _tableDatasource;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //[[SPLocationManager locationManager] startUpdatingLocationForSig];
    [self setNeedsStatusBarAppearanceUpdate];
    self.view.backgroundColor = [UIColor colorWithRed:230.0f/255.0f green:230.0f/255.0f blue:230.0f/255.0f alpha:1.0];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - kTopBarHeight)];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    if (self.creationView.center.y > self.view.frame.size.height) {
        self.creationView = nil;
    }
    NSLog(@"Mem warning");
}


//****************************************
//****************************************
#pragma mark - UITableViewDelegate/DataSource
//****************************************
//****************************************

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"INDEX PATH ROW: %d AND SECTION: %d", indexPath.row, indexPath.section);
    if (indexPath.section == 0) {
        UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SPMapCellSpace"];
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView = [[UIView alloc] init];
        cell.selectedBackgroundView = [[UIView alloc] init];
        return cell;
    } else if (indexPath.section == self.tableDatasource.count + 1) {
        UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SPBottomCellSpace"];
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView = [[UIView alloc] init];
        cell.selectedBackgroundView = [[UIView alloc] init];

        return cell;
    }
    SPMark* mark = self.tableDatasource[indexPath.section - 1];
    NSString* reuseId = [SPHomeViewController cellIdentifierFromData:mark];
    SPTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [SPTableViewCell cellFromMark:mark reuseID:reuseId];
        [cell updateView:YES];
    }
    [cell addDataToCell:mark];
    if (indexPath.section >= self.tableDatasource.count - 2 && !self.isLoadingData && self.pageNumber != -1) {
        self.fetchNextPage = YES;  // When the scrollview stops it will load more data if available.
    }

    return cell;
}

- (unsigned int)getPageNumber {
    return (self.userFilterSegment.selectedSegmentIndex == 0) ? self.pageNumber : self.friendsPageNumber;
}

- (void)setCurrentPageNumber:(unsigned int)page {
    if (self.userFilterSegment.selectedSegmentIndex == 0) {
        self.pageNumber = page;
    } else {
        self.friendsPageNumber = page;
    }
}

- (void)incrementCurrentPageNumber {
    if (self.userFilterSegment.selectedSegmentIndex == 0) {
        self.pageNumber++;
    } else {
        self.friendsPageNumber++;
    }
}

// Every cell has a section header so this should be equal to the number of speks returned from the server
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"section count is: %d",self.tableDatasource.count + 2 );
    return self.tableDatasource.count + 2;  // Add two because the mapview needs to go on the top and extra spacing at the bottom.
}

// There is a section for every cell, so there is only one cell per section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return kMapHeight+2;
    } else if (indexPath.section == self.tableDatasource.count + 1) {
        return kExtraSpaceBelowHomeView;
    }
    SPMark* mark = self.tableDatasource[indexPath.section - 1];
    return [SPTableViewCell cellHeightForMark:mark];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == self.tableDatasource.count + 1) {
        cell.backgroundColor = [UIColor clearColor];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == self.tableDatasource.count + 1)
        return;
    SPMark* mark = self.datasource[indexPath.section - 1 ];
    SPMarkViewController* markVC = [SPMarkViewController withMark:mark];
    [markVC displayData];
    [self.navigationController pushViewController:markVC animated:YES];
}

-(void)reloadTableview {
    [self.tableView setDelegate:self];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self.tableView setNeedsDisplay];
    });
}

- (void)showNoItems {
    if (self.tableDatasource.count == 0 && self.accuracyBad == NO) {
        self.opaqueIcon.hidden = NO;
        self.noItems.hidden = NO;
        self.beTheFirst.hidden = NO;
        self.downArrow.hidden = NO;
        self.noItemsBackround.hidden = NO;
        [self.view bringSubviewToFront:self.noItemsBackround];
        [self.view bringSubviewToFront:self.downArrow];
        [self.view bringSubviewToFront:self.beTheFirst];
        [self.view bringSubviewToFront:self.noItems];
        [self.view bringSubviewToFront:self.opaqueIcon];

    }
}

- (void)showTableView {
    if (self.tableDatasource.count != 0) {
        self.noItems.hidden = YES;
        self.beTheFirst.hidden = YES;
        self.downArrow.hidden = YES;
        self.noItemsBackround.hidden = YES;
        self.opaqueIcon.hidden = YES;

        [self.view sendSubviewToBack:self.noItemsBackround];
        [self.view sendSubviewToBack:self.downArrow];
        [self.view sendSubviewToBack:self.beTheFirst];
        [self.view sendSubviewToBack:self.noItems];
        [self.view sendSubviewToBack:self.opaqueIcon];
    }
}

//****************************************
//****************************************
#pragma mark - Setters/Getters
//****************************************
//****************************************

- (NSMutableArray*)datasource {
    if (!_datasource) {
        _datasource = [NSMutableArray array];
        if (!self.firstLoad) {
            [self loadDataForPagination:NO];
        }
    }
    return _datasource;
}

- (NSMutableArray*)friendsDatasource {
    if (!_friendsDatasource) {
        _friendsDatasource = [NSMutableArray array];
        if (!self.firstLoad) {
            [self loadDataForPagination:NO];
        }
    }
    return _friendsDatasource;
}

- (NSMutableArray*)tableDatasource {
    if (!_tableDatasource) {
        _tableDatasource = (self.userFilterSegment.selectedSegmentIndex == 0) ? self.datasource : self.friendsDatasource;
    }
    return _tableDatasource;
}

- (SPCreationView*)creationView {
    if (!_creationView) {
        UIView* window = [SPUtils getAppDelegate].window;
        CGSize viewSize = window.frame.size;
        CGRect startFrame = CGRectMake(0, viewSize.height, [SPUtils screenWidth], [SPUtils screenHeight]);
        _creationView = [SPCreationView creationView:startFrame delegate:self];
        [window insertSubview:_creationView belowSubview:self.creationButton];
        _creationView.frame = startFrame;
    }
    return _creationView;
}

- (void)setTableDatasource:(NSMutableArray *)tableDatasource {
    _tableDatasource = tableDatasource;
    [self preFetchImages];
    dispatch_async(dispatch_get_main_queue(), ^{
        if(_tableDatasource == nil || _tableDatasource.count == 0) {
            [self showNoItems];
        } else {
            [self showTableView];
        }
        [self reloadTableview];
    });
}

- (void)setDatasource:(NSMutableArray *)datasource {
    _datasource = datasource;
}

- (void)setFriendsDatasource:(NSMutableArray *)friendsDatasource {
    _friendsDatasource = friendsDatasource;
}


@end

最后,如果您认为这与我们的委托有关,那么我们不会触摸那里的视图控制器,因此我不认为这可能是一个问题.也许是内存问题或后台线程问题?

Lastly, if you think it has something to do with our delegate, we don't touch the view controller there, so I don't see how that could be a problem. Maybe a memory issue or a background thread issue?

推荐答案

问题是同时执行的后台线程过多.当我们停止尝试预取图像时,问题就消失了.

The problem was too many background threads going on at once. When we stopped trying to prefetch images, the issue went away.

这篇关于UITableView单元格奇怪地消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆