当条件遍历NSArray时创建新的UILabel

时间:2019-01-07 18:27:35

标签: ios objective-c nsarray enumerate

我有一个NSArray,各个部分除以..

此数组更长

citiesArray10000 = [NSArray arrayWithObjects:
                            @"33.572162&-112.087966&Phoenix&Arizona",
                            @"32.154289&-110.871062&735&Tucson Arizona ",
                            @"33.401926&-111.717379&Mesa&Arizona",
                            @"33.282874&-111.854943&Chandler&Arizona",
                            nil];

我遍历这些,看它们是否满足某些条件。

[citiesArray10000 enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
        // do something with object
        NSArray *coorArray = [object componentsSeparatedByString:@"&"];
        NSString *firstString = [coorArray objectAtIndex:0];
        NSString *secondString = [coorArray objectAtIndex:1];
        NSString *thirdString = [coorArray objectAtIndex:2];
        NSString *fourthString = [coorArray objectAtIndex:3];

if (fabs(crnLoc.coordinate.latitude - latitude) <= 1) {
            if (abs(crnLoc.coordinate.longitude - longitude <= 1)) {

                self.label.text = fourthString;
            }
        }

根据符合条件的对象的坐标,标签将浮在屏幕表面

self.label.frame = CGRectMake(160,(((self.mheading-90)-β)*-5.688)+200, 30, 200);

self.label2.frame = CGRectMake(160,(((self.mheading-90)-β)*-5.688)+200, 30, 200);

其中β的坐标值不同。

问题是,如果数组中有多个对象满足条件,则需要创建另一个标签,并将其文本作为该对象的fourString。然后,当条件不再满足时,删除该标签。反正有这样做吗?

1 个答案:

答案 0 :(得分:0)

如何使用标签? 当您遍历cityArray10000数组时,我认为您可以将标签添加到uilabel中,然后在条件不再满足时,可以通过该标签找到该标签并删除该标签。

[citiesArray10000 enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
        // do something with object
        NSArray *coorArray = [object componentsSeparatedByString:@"&"];
        NSString *firstString = [coorArray objectAtIndex:0];
        NSString *secondString = [coorArray objectAtIndex:1];
        NSString *thirdString = [coorArray objectAtIndex:2];
        NSString *fourthString = [coorArray objectAtIndex:3];

if (fabs(crnLoc.coordinate.latitude - latitude) <= 1) {
            if (abs(crnLoc.coordinate.longitude - longitude <= 1)) {

                UILabel *label = (UILabel *)[self.view viewWithTag:idx];

                label.text = fourthString;

                // add the label to the view ...
            }
        }
    }