Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSMUTABLEARRY按距离排序的数据_Ios_Objective C_Nsmutablearray - Fatal编程技术网

Ios NSMUTABLEARRY按距离排序的数据

Ios NSMUTABLEARRY按距离排序的数据,ios,objective-c,nsmutablearray,Ios,Objective C,Nsmutablearray,我的可变数组有问题。我的tableview中有数据数组,一切正常,但当数据按距离排序时,我在单元格中选择一项时,它会在新的viewcontroller中显示不正确的数据。它显示数据而不进行排序。这就像一个断开的链接)希望你的帮助) 我是obj-c的新手,所以我道歉) 这是我的密码: list = [[NSMutableArray alloc] init]; [list addObject:@{@"name": @"Central office", @"addres

我的可变数组有问题。我的tableview中有数据数组,一切正常,但当数据按距离排序时,我在单元格中选择一项时,它会在新的viewcontroller中显示不正确的数据。它显示数据而不进行排序。这就像一个断开的链接)希望你的帮助)

我是obj-c的新手,所以我道歉)

这是我的密码:

       list = [[NSMutableArray alloc] init];

        [list addObject:@{@"name": @"Central office", @"address":@"наб. Обводного канала, д.66А", @"phone":@"+7 (812) 320-56-21 (многоканальный)", @"workTime":@"ПН-ПТ: с 9:30 до 18:30", @"email":@"mail@ibins.ru", @"payment":@"Принимаются к оплате пластиковые карты VISA и MasterCard", @"longitude":@30.336842, @"latitude":@59.913950}];

        [list addObject:@{@"name": @"Second office", @"address":@"ул. Камышовая, д.38", @"phone":@"+7 (812) 992-992-6; +7 (812) 456-26-43", @"workTime":@"Ежедневно: с 9:30 до 20:00", @"email":@"sever@ibins.ru", @"payment":@"Принимаются к оплате пластиковые карты VISA и MasterCard", @"longitude":@30.219863, @"latitude":@60.008805}];

        [list addObject:@{@"name": @"Third office", @"address":@"Street name", @"phone":@"phone number", @"workTime":@"Work time", @"email":@"email address", @"longitude":@30.294254, @"latitude":@60.028728}]; 

    [self constructList];
    [self constructPins];
    }



- (IBAction)switchDisplayType:(id)sender {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.80];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
    [UIView commitAnimations];

    if ([(UISegmentedControl*)sender selectedSegmentIndex] == 1) {
        map.hidden = YES;
        contentSV.hidden = NO;
    }
    else {
        map.hidden = NO;
        contentSV.hidden = YES;
    }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier";

    MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                           initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
    customPinView.pinColor = MKPinAnnotationColorRed;
    customPinView.animatesDrop = YES;
    customPinView.canShowCallout = YES;

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
   // NSLog(@"%@",rightButton);
    [rightButton addTarget:self
                    action:@selector(annotationButtonTapped:)
          forControlEvents:UIControlEventTouchUpInside];
    customPinView.rightCalloutAccessoryView = rightButton;
    return customPinView;//[kml viewForAnnotation:annotation];
}

- (void)annotationButtonTapped:(id)sender {
    DetailVC *sampleVC = [[DetailVC alloc] initWithNibName:@"DetailVC" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:sampleVC animated:YES];
    [sampleVC release];

    for (NSDictionary *dict in list) {
        if ([[dict valueForKey:@"name"] isEqualToString:selectedAnnTitle]) {
            [sampleVC updateViewWithDict:dict];
        }
    }
}



- (void)constructPins {
    for (NSDictionary *dict in list) {
        MKCoordinateRegion region;
        MKCoordinateSpan span;
        span.latitudeDelta = 0.3;
        span.longitudeDelta = 0.3;

        CLLocationCoordinate2D location;

        location.latitude = [[dict valueForKey:@"latitude"] floatValue];
        location.longitude = [[dict valueForKey:@"longitude"] floatValue];

        if (location.latitude == 0.0 && location.longitude == 0.0)
            return;

        region.span = span;
        region.center = location;

        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = location;
        point.title = [dict valueForKey:@"name"];

        [map addAnnotation:point];
        [map setRegion:region animated:YES];
    }
}

- (void)constructList {
    int n = 0;
    for (NSDictionary *dict in list) {
        UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 100*n, 320, 100)];
        container.backgroundColor = [UIColor whiteColor];

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
        button.tag = n;
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [container addSubview:button];


        NSString *str = [NSString stringWithFormat:@"%@\nAddress: %@\nPhone: %@\nWork Time: %@", [dict valueForKey:@"name"], [dict valueForKey:@"address"], [dict valueForKey:@"phone"], [dict valueForKey:@"workTime"]];
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 80)];
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.numberOfLines = 0;
        nameLabel.text = str;
        nameLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
        [container addSubview:nameLabel];


        UIView *separator = [[UIView alloc]initWithFrame:CGRectMake(0, 99, 320, 1)];
        separator.backgroundColor = [UIColor darkGrayColor];
        [container addSubview:separator];


        [contentSV addSubview:container];


        n++;
        contentSV.contentSize = CGSizeMake(0, 100*n);
    }
}

- (float)distanceBetweenLat1:(float)tlat1 lon1:(float)tlon1 lat2:(float)tlat2 lon2:(float)tlon2 {
    float result = 0.0;
    int R = 6371;

    float currentLatitude = tlat1;
    float currentLongtitude = tlon1;

    float lat2 = tlat2;
    float lon2 = tlon2;

    float dLat = (lat2-currentLatitude)*M_PI/180;
    float dLon = (lon2-currentLongtitude)*M_PI/180;
    float nlLat = currentLatitude*M_PI/180;
    lat2 = lat2*M_PI/180;

    float a = sin(dLat/2) * sin(dLat/2) +  sin(dLon/2) * sin(dLon/2) * cos(nlLat) * cos(lat2);
    float c = 2 * atan2(sqrt(a), sqrt(1-a));
    result = R * c;

    return result;
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    selectedAnnTitle = [view.annotation title];

    return;

    DetailVC *sampleVC = [[DetailVC alloc] initWithNibName:@"DetailVC" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:sampleVC animated:YES];


    NSString *ttl = [view.annotation title];

    for (NSDictionary *dict in list) {
        if ([[dict valueForKey:@"name"] isEqualToString:ttl]) {
            [sampleVC updateViewWithDict:dict];
        }
    }

    [map deselectAnnotation:view.annotation animated:NO];
}

- (void)buttonTapped:(id)sender {
    DetailVC *sampleVC = [[DetailVC alloc] initWithNibName:@"DetailVC" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:sampleVC animated:YES];


    int n = 0;
    for (NSDictionary *dict in list) {
        if (n == [sender tag]) {
            [sampleVC updateViewWithDict:dict];
        }
        n++;
    }
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    NSLog(@"didUpdateUserLocation");

    MKAnnotationView* annotationView = [mapView viewForAnnotation:userLocation];
    annotationView.canShowCallout = NO;

    if (!userLocationUpdated) {
        if (userLocation.coordinate.latitude > 0.1 && userLocation.coordinate.longitude > 0.1) {
         //   NSLog(@"SORT BY DISTANCE");

            userLocationUpdated = YES;

            for (int i = 0; i < [list count]; i++) {
                NSMutableDictionary *record = [[NSMutableDictionary alloc] initWithDictionary:[list objectAtIndex:i]];

                float latitude = [[record valueForKey:@"latitude"] floatValue];
                float longitude = [[record valueForKey:@"longitude"] floatValue];

                float dist = [self distanceBetweenLat1:map.userLocation.coordinate.latitude lon1:map.userLocation.coordinate.longitude lat2:latitude lon2:longitude];
                NSNumber *distN = [NSNumber numberWithFloat:dist];
                [record setObject:distN forKey:@"distance"];

              [list replaceObjectAtIndex:i withObject:record];
            }

            NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
            NSArray *itemsListSorted = [[NSArray alloc] initWithArray:list];
            itemsListSorted = [itemsListSorted sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

            for (UIView *view in contentSV.subviews) {
                [view removeFromSuperview];
            }

            for (int i = 0; i < [itemsListSorted count]; i++) {
                UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 100*i, 320, 100)];
                container.backgroundColor = [UIColor whiteColor];

                UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
                button.tag = i;
                [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
                [container addSubview:button];


                NSString *str = [NSString stringWithFormat:@"%@\nAddress: %@\nPhone: %@\nWorkTime: %@", [[itemsListSorted objectAtIndex:i] valueForKey:@"name"], [[itemsListSorted objectAtIndex:i] valueForKey:@"address"], [[itemsListSorted objectAtIndex:i] valueForKey:@"phone"], [[itemsListSorted objectAtIndex:i] valueForKey:@"workTime"]];
                UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 80)];
                nameLabel.textAlignment = NSTextAlignmentLeft;
                nameLabel.numberOfLines = 0;
                nameLabel.text = str;
                nameLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
                [container addSubview:nameLabel];


                UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 300, 21)];
                distanceLabel.textAlignment = NSTextAlignmentRight;
                distanceLabel.text = [NSString stringWithFormat:@"%.1f км", [[[itemsListSorted objectAtIndex:i] valueForKey:@"distance"] floatValue]];
                distanceLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
                distanceLabel.textColor = [UIColor lightGrayColor];
                [container addSubview:distanceLabel];


                UIView *separator = [[UIView alloc]initWithFrame:CGRectMake(0, 99, 320, 1)];
                separator.backgroundColor = [UIColor darkGrayColor];
                [container addSubview:separator];


                [contentSV addSubview:container];


                contentSV.contentSize = CGSizeMake(0, 100*(i+1));
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
list=[[NSMutableArray alloc]init];
[列表添加对象:{“姓名”:“中央办公室”,地址:“ааааааааааааааааа,а.66А”,“电话:“+7(812)320-56-21(аааааааааааmail@ibins.ru“,@“付款”:@“经度”:@30.336842,“纬度”:@59.913950}];
[列表添加对象:{“姓名”:“第二办公室”、“地址”:“电话”:“+7(812)992-992-6”;+7(812)456-26-43”;“工作时间”:“9:30”“20:00”;“电子邮件”:sever@ibins.ru“付款”:“经度”“:@30.219863,@“纬度”:@60.008805}];
[列表添加对象:@{“姓名”:“第三个办公室”,“地址”:“街道名称”,“电话”:“电话号码”,“工作时间”:“工作时间”,“电子邮件”:“电子邮件地址”,“经度”:@30.294254”,“纬度”:@60.028728}];
[自我建构清单];
[自我建构];
}
-(iAction)switchDisplayType:(id)发送方{
[UIView beginAnimations:@“视图翻转”上下文:nil];
[UIView设置动画持续时间:0.80];
[UIView设置动画曲线:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view缓存:否];
[UIView委员会];
如果([(UISegmentedControl*)发送方选择了分段索引]==1){
map.hidden=是;
contentSV.hidden=否;
}
否则{
map.hidden=否;
contentSV.hidden=是;
}
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
静态NSString*BridgeAnnotationIdentifier=@“BridgeAnnotationIdentifier”;
MKPinAnnotationView*自定义PinView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation重用标识符:BridgeAnnotationIdentifier]autorelease];
customPinView.pinColor=MKPinAnnotationColorRed;
customPinView.animatesDrop=是;
customPinView.canShowCallout=是;
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
//NSLog(@“%@”,右键);
[rightButton添加目标:自我
操作:@选择器(annotationButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView=rightButton;
返回customPinView;//[kml viewForAnnotation:annotation];
}
-(无效)注释按钮已标记:(id)发件人{
DetailVC*sampleVC=[[DetailVC alloc]initWithNibName:@“DetailVC”bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:sampleVC动画:是];
[样本VC发布];
用于(NSDictionary*列表中的dict){
if([[dict valueForKey:@“name”]isEqualToString:selectedAnnTitle]){
[sampleVC updateViewWithDict:dict];
}
}
}
-(无效)构造销{
用于(NSDictionary*列表中的dict){
协调区域;
Mk坐标跨度;
span.latitudeDelta=0.3;
span.longitudeDelta=0.3;
CLLOCATION坐标2D定位;
location.latitude=[[dict valueForKey:@“latitude”]floatValue];
location.longitude=[[dict valueForKey:@“longitude”]floatValue];
if(location.latitude==0.0&&location.longitude==0.0)
返回;
region.span=span;
region.center=位置;
MKPointAnnotation*point=[[MKPointAnnotation alloc]init];
点坐标=位置;
point.title=[dict valueForKey:@“name”];
[地图添加注释:点];
[地图设置区域:区域动画:是];
}
}
-(作废)施工清单{
int n=0;
用于(NSDictionary*列表中的dict){
UIView*container=[[UIView alloc]initWithFrame:CGRectMake(01000*n320100)];
container.backgroundColor=[UIColor whiteColor];
UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(0,032100)];
button.tag=n;
[按钮添加目标:自我操作:@选择器(按钮标记:)用于控制事件:UIControlEventTouchUpInside];
[容器添加子视图:按钮];
NSString*str=[NSString stringWithFormat:@“%@\n地址:%@\n电话:%@\n工作时间:%@“,[dict valueForKey:@“姓名”],[dict valueForKey:@“地址”],[dict valueForKey:@“电话”],[dict valueForKey:@“工作时间”];
UILabel*nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,10300,80)];
nameLabel.textAlignment=NSTextAlignmentLeft;
nameLabel.numberOfLines=0;
namelab.text=str;
nameLabel.font=[UIFont fontWithName:@“HelveticaNeue”大小:12.0];
[容器添加子视图:名称标签];
UIView*分隔符=[[UIView alloc]initWithFrame:CGRectMake(0,99320,1)];
separator.backgroundColor=[UIColor darkGrayColor];
[容器添加子视图:分隔符];
[contentSV addSubview:container];
n++;
contentSV.contentSize=CGSizeMake(0,100*n);
}
}
-(浮动)LAT1:(浮动)tlat1 lon1:(浮动)tlon1 lat2:(浮动)tlat2 lon2:(浮动)tlon2之间的距离{
浮动结果=0.0;
int R=6371;
浮动电流纬度=tlat1;
浮动电流长度=tlon1;
浮动lat2=tlat2;
浮点数lon2=tlon2;
浮动dLat=(lat2当前纬度)*M_PI/180;
浮点数dOn=(lon2电流长度)*M_PI/180;
float nlLat=当前纬度*M_PI/180;
lat2=lat2*M_PI/180;
浮点数a=sin(dLat/2)*sin(dLat/2)+sin(d
NSArray *itemsListSorted = [[NSArray alloc] initWithArray:list];
itemsListSorted = [itemsListSorted sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
[list sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];