Hai我是xcode的新手我使用UITableView来显示车辆编号及其位置。它使用网络来查找位置。我的问题是每当我滚动表时,只有它从网上重新加载位置。是否可以首次加载数据并仅绑定表一次。然后我就可以顺利滚动。亲切地告诉我,我的代码如下......
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;
}
提前致谢..
答案 0 :(得分:0)
主线程用于接口操作。如果您发布网络请求,它会被阻止,因为它正在等待网络。 将它发布在后台线程中。做一些关于如何使用Grand Central Dispatch,块和并发编程的研究。这些部分将帮助你。
答案 1 :(得分:0)
好的做法是从你的"喂养"以外的地方获取你的数据。方法。你可以这样继续:
位于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....
}
}
基本上,每次显示一个单元格时都会从网络上获取一些数据,这就是为什么你的视图看起来很慢。