当滚动到表格末尾时,ios 8 uitableview卡住/挂起/冻结

时间:2014-11-28 03:10:59

标签: ios objective-c iphone uitableview

我真的需要你的帮助。我试图将一些数据(大约50条记录)加载到UITableView。但是在滚动到tableview结束时它被冻结/挂起。我确实尝试记录显示哪一行。当我向下滚动时,日志显示第1行... 49显示然后它从第42行开始。

这是.h文件:

@interface VDevicesViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
    NSString* access_token;
    NSMutableArray* data;
    NSMutableArray* deviceNotGoodData;
    NSArray* _deviceTypeList;
    NSArray* _thumnailArray;
    DAPagesContainer* pageContainter;
    UITableView* tableAllDevices;
    UITableView* tableNotGoodDevices;

    int currentPage;
    UIView * footerView;
    UIActivityIndicatorView* indicator;
}
@property (strong, nonatomic) IBOutlet UIView *myView;

-(void)refresh;
@end

这是.m文件

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [self.myView setFrame:CGRectMake(0, 32, screenRect.size.width, screenRect.size.height)];

    // Do any additional setup after loading the view
    data = [[NSMutableArray alloc] init];
    deviceNotGoodData = [[NSMutableArray alloc] init];
    pageContainter = [[DAPagesContainer alloc] init];
    [pageContainter setPageIndicatorImage:[UIImage imageNamed:@"up_arrow_gray"]];
    [pageContainter setTopBarBackgroundColor:[UIColor colorWithRed:24.0f/255.0f green:177.0f/255.0f blue:214.0f/255.0f alpha:1]];
    pageContainter.pageItemsTitleColor = [UIColor whiteColor];

    [pageContainter willMoveToParentViewController:self];
    pageContainter.view.frame = self.myView.bounds;
    pageContainter.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    CGFloat width = self.myView.frame.size.width;
    CGFloat height = self.myView.frame.size.height - 50;
    CGRect tableFrame = CGRectMake(0, 0, width, height);
    UIViewController* allDeviceContoler = [[UIViewController alloc] init];
    tableAllDevices = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    tableAllDevices.delegate = self;
    tableAllDevices.dataSource = self;
    tableAllDevices.tag = 0;

    NSString* allDeviceTitle = [NSString stringWithFormat:@"All Devices(%@)", [[NSUserDefaults standardUserDefaults] valueForKey:KEY_DEVICES_USED]];
    allDeviceContoler.title = allDeviceTitle;
    [allDeviceContoler.view addSubview:tableAllDevices];

    UIViewController *notGoodDevicesControler = [[UIViewController alloc] init];
    tableNotGoodDevices = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    tableNotGoodDevices.delegate = self;
    tableNotGoodDevices.dataSource = self;
    tableNotGoodDevices.tag = 1;

    NSString* uncompliantDevices = [NSString stringWithFormat:@"With Issues(%@)", [[NSUserDefaults standardUserDefaults] valueForKey:KEY_DEVICES_UNCOMPLIANT]];
    notGoodDevicesControler.title = uncompliantDevices;
    [notGoodDevicesControler.view addSubview:tableNotGoodDevices];

    [self.myView addSubview:pageContainter.view];
    [pageContainter didMoveToParentViewController:self];
    pageContainter.viewControllers = @[allDeviceContoler, notGoodDevicesControler];
    currentPage = 1;
    access_token = [[NSUserDefaults standardUserDefaults] valueForKey:TOKEN_ACCESS_KEY];
    _deviceTypeList = [NSArray arrayWithObjects:@"desktop", @"laptop", @"phone", @"tablet", @"server", @"router", @"switch", @"vm", nil];

    _thumnailArray = [NSArray arrayWithObjects:@"device_desktop_small", @"device_laptop_small", @"phone", @"tablet", @"device_server_small", @"router", @"switch", @"device_vm_small", nil];

    [self GetDevices:[NSNumber numberWithInt:currentPage]];
    indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(screenRect.size.width/2.0, 40, 37, 37)];
    [indicator startAnimating];

    [self.myView addSubview:indicator];
}

#pragma mark - TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 0) {
        return [data count];
    }
    else if (tableView.tag == 1)
        return [deviceNotGoodData count];
    return 0;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* cellIdentitifier = @"TableCell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentitifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentitifier];

    }

    NSArray* flexData;
    if (tableView.tag == 0) {
        flexData = [NSArray arrayWithArray:data];
    }
    else
        flexData = [NSArray arrayWithArray:deviceNotGoodData];


    if (flexData.count > 0) {
        if (indexPath.row < ([flexData count])) {
            // add data here
            NSLog(@"%ld", (long)indexPath.row);
            VDeviceObject * deviceObject = [flexData objectAtIndex:(indexPath.row)];
            cell.textLabel.text = deviceObject.hostname;
            cell.detailTextLabel.text = deviceObject.username;
            cell.detailTextLabel.textColor = [UIColor colorWithRed:180.0f/255.0f green:180.0f/255.0f blue:180.0f/255.0f alpha:1];

            if ([deviceObject.numberOfIssues intValue] > 0) {
                cell.backgroundColor = [UIColor colorWithRed:247.0f/255.0f green:247.0f/255.0f blue:247.0f/255.0f alpha:1];
            }

            cell.imageView.image = [UIImage imageNamed:[_thumnailArray objectAtIndex:[deviceObject.deviceType intValue]]];
        }
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier: @"ViewDetail" sender:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
-(void)GetDevices:(NSNumber*)page {
    if (access_token == nil || [access_token isEqualToString:@""]) {
        return;
    }
    NSError* error;
    NSData * devicesData = [VTabBarViewController sendSyncGetRequest:[NSString stringWithFormat:@"%@?access_token=%@&limit=%@&GroupBy=issues&page=%@", GEARS_API_GET_DEVICES, access_token, @"50", page] errorCode:error];

    NSString* errorString = [[NSString alloc] initWithData:devicesData encoding:NSUTF8StringEncoding];
    NSString* stringCode = @"Requested page number exceeds limit";
    if ([errorString rangeOfString:stringCode].location != NSNotFound) {
        return;
    }
    NSMutableArray* list = [NSJSONSerialization JSONObjectWithData:devicesData options:NSJSONReadingAllowFragments error:&error];
    for (int i = 0 ; i<[list count]; ++i) {
        NSDictionary *dict = [list objectAtIndex:i];
        NSString* hostName = [dict objectForKey:@"hostname"];
        NSString *machine_type = [dict objectForKey:KEY_MACHINE_TYPE];
        NSNumber* numberIssues = [dict objectForKey:KEY_TOTAL_ISSUE];
        NSDictionary *userinfo = [dict objectForKey:KEY_USER_INFO];
        NSString* username = [userinfo objectForKey:KEY_USER_NAME];
        NSString* hwid = [dict objectForKey:KEY_HWID];
        VDeviceObject *object = [[VDeviceObject alloc] init];
        object.username = username;
        object.deviceType = [self deviceType:machine_type];
        object.numberOfIssues = numberIssues;
        object.hostname = hostName;
        object.hardwareId = hwid;

        [data addObject:object];
        if ([object.numberOfIssues intValue] > 0) {
            [deviceNotGoodData addObject:object];
        }
    }
    NSLog(@"On page: %d", currentPage);
    //[tableAllDevices reloadData];
    //[tableNotGoodDevices reloadData];
}

这是展示我遇到的问题的gif图片(我确实尝试减小尺寸,但质量太差了) http://s934.photobucket.com/user/quocvinh_2009/media/TableView%20Bug.gif.html

谢谢...

更新:我尝试添加&#39; UITableView&#39;通过故事板,连接到&#39; IBOutlet&#39;,它的工作原理。所以也许我在桌子上做错了什么:(

2 个答案:

答案 0 :(得分:0)

显示前41行的数据,所以我假设deliegates没问题。

问题在第42行上升。您当时可能会显示7行或8行。因此在显示第49行或第50行时会出现问题。根据您的规格不存在。然而,UITableView正在尝试获取该行。

我建议检查表计数:

NSLog(@"Rows %d",[tablename count]);

并检查UITableView数据源长度是否正确:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableArray count];
}

答案 1 :(得分:0)

感谢帮助我。 正确的问题是因为我用于这个项目的开源。 以下是需要

的人的参考https://github.com/daria-kopaliani/DAPagesContainer/issues/9