UITableView 滚动非常慢,每次滚动都使用网络 [英] UITableView scrolling is very slow it use the network for every scrolling
问题描述
我是 xcode 的新手,我使用 UITableView 来显示车辆编号及其位置.它使用网络来查找位置.我的问题是每次我滚动表格时只有它从网上重新加载位置.是否可以第一次加载数据并只绑定一次表.然后我可以顺利滚动.请建议我我的代码如下...
Hai I am new to xcode i use a UITableView to display the vehicle number and its location. it use network for find the location. My problem is every time i scroll the table that time only it reloads the location from the net. Is it possible to load the datas for the first time and bind the table only once. Then I can scroll smoothly.Kindly advice me my code is bellow...
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;
}
提前致谢..
推荐答案
好的做法是从feeding"方法外部获取数据.你可以这样继续:
The good practice is to fetch your data outside from your "feeding" method. You can proceed like this:
在 ViewController
的顶部:
@implementation MyCustomViewController {
NSMutableArray *storeArray;
UITableView *myTableView;
}
在你的视图 viewDidLoad
中初始化你的数组,初始化你的 UITableView
并运行一个填充它的方法:
In your view viewDidLoad
init your array, init your UITableView
and run a method which is filling it:
- (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];
}
实现用于下载数据的代码:
Implement your code for downloading your data:
- (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 协议
并用 storeArray
中的数据填充你的单元格:
Finally, implement your UITableView protocol
and fill your cell with the data in your 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....
}
}
基本上,每次显示单元格时都会从网络获取一些数据,这就是您的视图看起来很慢的原因.
Basically, some data are fetched from the web every time a cell is displayed, that's why your view appear to be very slow.
这篇关于UITableView 滚动非常慢,每次滚动都使用网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!