UITableView滚动是非常慢的,它使用每个滚动的networking

海我是新的xcode我使用UITableView来显示车辆号码和它的位置。 它使用networking来查找位置。 我的问题是我每次滚动表时,只有它从网上重新加载的位置。 是否有可能第一次加载数据,并绑定表只有一次。 然后,我可以顺利滚动。请给我build议我的代码是波纹pipe…

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MainCell"]; if(cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; } NSString *Vehicleno=[Vehicle_No objectAtIndex:indexPath.row]; NSString *urlMapString=[NSString stringWithFormat:@"http://logix.com/logix_webservice/map.php?format=json&truckno=%@",Vehicleno]; NSURL *urlMap=[NSURL URLWithString:urlMapString]; NSData *dataMap=[NSData dataWithContentsOfURL:urlMap]; NSError *errorMap; NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap]; NSArray *resultsMap = [jsonMap valueForKey:@"posts"]; NSArray *resMap = [resultsMap valueForKey:@"post"]; NSArray *latitudeString=[resMap valueForKey:@"latitude"]; if([latitudeString count]>0){ NSString *latOrgstring = [latitudeString objectAtIndex:0]; double latitude=[latOrgstring doubleValue]; NSArray *longitudeString=[resMap valueForKey:@"longitude"]; NSString *longOrgstring = [longitudeString objectAtIndex:0]; double longitude=[longOrgstring doubleValue]; //MAP VIEW Point MKCoordinateRegion myRegion; //Center CLLocationCoordinate2D center; center.latitude=latitude; center.longitude=longitude; //Span MKCoordinateSpan span; span.latitudeDelta=THE_SPAN; span.longitudeDelta=THE_SPAN; myRegion.center=center; myRegion.span=span; //Set our mapView [MapViewC setRegion:myRegion animated:NO]; CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude]; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) { if ([placemarks count] > 0) { NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary]; addressOutlet=[dictionary valueForKey:@"Street"]; City=[dictionary valueForKey:@"City"]; State=[dictionary valueForKey:@"State"]; if (addressOutlet!=NULL&&City!=NULL) { NSString *SubTitle=[NSString stringWithFormat:@"%@,%@,%@",addressOutlet,City,State]; cell.detailTextLabel.text=SubTitle; } else if (addressOutlet==NULL&&City!=NULL) { NSString *SubTitle=[NSString stringWithFormat:@"%@,%@",City,State]; cell.detailTextLabel.text=SubTitle; } else if (addressOutlet!=NULL&&City==NULL) { NSString *SubTitle=[NSString stringWithFormat:@"%@,%@",addressOutlet,State]; cell.detailTextLabel.text=SubTitle; } else if(addressOutlet==NULL&&City==NULL&&State!=NULL) { NSString *SubTitle=[NSString stringWithFormat:@"%@",State]; cell.detailTextLabel.text=SubTitle; } else if (addressOutlet==NULL&&City==NULL&&State==NULL) { NSString *SubTitle=[NSString stringWithFormat:@"%@",@""]; cell.detailTextLabel.text=SubTitle; } } }]; } NSUInteger row = [indexPath row]; cell.textLabel.text=[Vehicle_No objectAtIndex:row]; cell.backgroundColor=[UIColor clearColor]; return cell; } 

提前致谢..

主线程用于接口操作。 如果您发布networking请求,则会被阻止,因为它正在等待networking。 将其张贴在后台线程中。 做一些关于如何使用Grand Central Dispatch,块和并发编程的研究。 这些部分将帮助你。

最好的做法是从你的“喂食”方法中取出你的数据。 你可以这样做:

在您的ViewController的顶部:

 @implementation MyCustomViewController { NSMutableArray *storeArray; UITableView *myTableView; } 

在你的视图viewDidLoad初始化你的数组,启动你的UITableView并运行一个正在填充它的方法:

 - (void)viewDidLoad { / Init your NSMutableArray storeArray = [[NSMutableArray alloc] init]; [super viewDidLoad]; // Do any additional setup after loading the view. // Init your tableView myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); myTableView.delegate = self; myTableView.datasource = self; [self addSubview:myTableView]; // Run your method [self downloadMyData]; } 

实施您的代码以下载您的数据:

 - (void)downloadMyData { // All your code here in order to download your data // And store them in your array [storeArray addObject:object]; // When you have all your data downloaded, you have to reload your tableview: [myTableView reloadData]; } 

最后,实现你的UITableView protocol并用你的storeArray的数据填充你的单元格:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MainCell"]; if(cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; // Access your data via your array data1 = [storeArray objectAtIndex:indexPath.row]; cell.dataX = data1 // ETC.... } } 

基本上,每次显示单元格时都会从网页中提取一些数据,这就是为什么您的视图显得非常慢。