lundi 1 décembre 2014

ios 8 uitableview stuck/hang/freeze when scrolling to the end of table

I really need helps from you. I'm trying to load some data (about 50 records) to UITableView. But it got frozen/hang while scrolling to the end of the tableview. I did try to log which row is displaying. When I scroll down, the log show the row 1... 49 is display then it starts over with row 42.


This is the .h file:



@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


this is the .m file



- (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];
}


This is the gif image to demonstrate the problem I've got (I did try to reduce the size but its quality is so bad) http://ift.tt/1zBy4Co


Thank you...


Update: I try to add 'UITableView' by storyboard, connect with the 'IBOutlet', it works. So maybe I did something wrong with the table :(




Aucun commentaire:

Enregistrer un commentaire