创建城市和州的字典,以便在UITableView中存储字符串

时间:2013-10-07 23:31:40

标签: ios uitableview nsarray nsdictionary

我正在创建一个带有状态列表的UITableView,一旦您在其中一个单元格中选择一个状态,它就会切换到下一个视图,该视图将包含与该状态对应的城市列表。到目前为止,这是我的一系列城市:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

NSMutableArray *mainArray = [[NSMutableArray alloc] init];


        self.section1 = @[@"Alabama",@"Alaska",@"Arizona",@"Arkansas",@"California",@"Colorado",@"Connecticut",@"Delaware"];
        self.section2 = @[@"Florida",@"Georgia",@"Hawaii",@"Idaho",@"Illinois",@"Indiana",@"Iowa",@"Kansas",@"Kentucky",@"Louisiana"];
        self.section3 = @[@"Maine",@"Maryland",@"Massachusetts",@"Michigan",@"Minnesota",@"Mississippi",@"Misouri",@"Montana"];
        self.section4 = @[@"Nevada",@"Nebraska",@"New Hampshire",@"New Jersey",@"New Mexico",@"New York",@"North Carolina",@"North Dakota",@"Ohio",@"Ohio",@"Oklahoma",@"Oregon"];
        self.section5 = @[@"Pennsylvania",@"Rhode Island",@"South Carolina",@"South Dakota",@"Tennessee",@"Texas",@"Utah"];
        self.section6 = @[@"Vermont",@"Virginia",@"Washington",@"West Virginia",@"Wisconsin",@"Wyoming"];
    }
    return self;
}

那么我是否必须创建一个单独的城市数组,然后将这两个数组合并为一个字典,然后相应地对列表进行排序?此外,我希望一旦选择单元格来存储并根据所选的城市或州传递字符串。

像这样:SanFrancisco.California

更新:

这里有一堆阵列,其中包含所有城市,Craigslist提供按州和城市分类的服务:

self.Alabama = @[@"auburn",
                          @"birmingham",
                          @"dothan",
                          @"florence / muscle shoals",
                          @"gadsden-anniston",
                          @"huntsville / decatur",
                          @"mobile",
                          @"montgomery",
                          @"tuscaloosa"];


        self.Alaska = @[@"anchorage / mat-su",
                        @"fairbanks",
                        @"kenai peninsula",
                        @"southeast alaska"];

        self.Arizona = @[@"flagstaff / sedona",
                         @"mohave county",
                         @"phoenix",
                         @"prescott",
                         @"show low",
                         @"sierra vista",
                         @"tucson",
                         @"yuma"];

        self.Arkansas = @[@"fayetteville",
                          @"fort smith",
                          @"jonesboro",
                          @"little rock",
                          @"texarkana"];

        self.California = @[@"bakersfield",
                            @"chico",
                            @"fresno / madera",
                            @"gold country",
                            @"hanford-corcoran",
                            @"humboldt county",
                            @"imperial county",
                            @"inland empire",
                            @"los angeles",
                            @"mendocino county",
                            @"merced",
                            @"modesto",
                            @"monterey bay",
                            @"orange county",
                            @"palm springs",
                            @"redding",
                            @"sacramento",
                            @"san diego",
                            @"san francisco bay area",
                            @"san luis obispo",
                            @"santa barbara",
                            @"santa maria",
                            @"siskiyou county",
                            @"stockton",
                            @"susanville",
                            @"ventura county",
                            @"visalia-tulare",
                            @"yuba-sutter"];

        self.Colorado = @[@"boulder",
                          @"colorado springs",
                          @"denver",
                          @"eastern CO",
                          @"fort collins / north CO",
                          @"high rockies",
                          @"pueblo",
                          @"western slope"];

        self.Connecticut = @[@"eastern CT",
                             @"hartford",
                             @"new haven",
                             @"northwest CT"];

        self.Delaware = @[@"delware"];

        self.Florida = @[@"daytona beach",
                         @"florida keys",
                         @"fort lauderdale",
                         @"ft myers / SW florida",
                         @"gainesville",
                         @"heartland florida",
                         @"jacksonville",
                         @"lakeland",
                         @"north central FL",
                         @"ocala",
                         @"okaloosa / walton",
                         @"orlando",
                         @"panama city",
                         @"pensacola",
                         @"sarasota-bradenton",
                         @"south florida",
                         @"space coast",
                         @"st augustine",
                         @"tallahassee",
                         @"tampa bay area",
                         @"treasure coast",
                         @"west palm beach"];

        self.Georgia = @[@"albany",
                         @"athens",
                         @"atlanta",
                         @"augusta",
                         @"brunswick",
                         @"columbus",
                         @"macon / warner robins",
                         @"northwest GA",
                         @"savannah / hinesville",
                         @"statesboro",
                         @"valdosta"];

        self.Hawaii = @[@"hawaii"];

        self.Idaho = @[@"boise",
                       @"east idaho",
                       @"lewiston / clarkston",
                       @"twin falls"];

        self.Illinois = @[@"bloomington-normal",
                          @"champaign urbana",
                          @"chicago",
                          @"decatur",
                          @"la salle co",
                          @"mattoon-charleston",
                          @"peoria",
                          @"rockford",
                          @"southern illinois",
                          @"springfield",
                          @"western IL"];

        self.Indiana = @[@"bloomington",
                         @"evansville",
                         @"fort wayne",
                         @"indianapolis",
                         @"kokomo",
                         @"lafayette / west lafayette",
                         @"muncie / anderson",
                         @"richmond",
                         @"south bend / michiana",
                         @"terre haute"];

        self.Iowa = @[@"ames",
                      @"cedar rapids",
                      @"des moines",
                      @"dubuque",
                      @"fort dodge",
                      @"iowa city",
                      @"mason city",
                      @"quad cities",
                      @"sioux city",
                      @"southeast IA",
                      @"waterloo / cedar falls"];

        self.Kansas = @[@"lawrence",
                        @"manhattan",
                        @"northwest KS",
                        @"salina",
                        @"southeast KS",
                        @"southwest KS",
                        @"topeka",
                        @"wichita"];

        self.Kentucky = @[@"bowling green",
                          @"eastern kentucky",
                          @"lexington",
                          @"louisville",
                          @"owensboro",
                          @"western KY"];

        self.Louisiana = @[@"baton rouge",
                           @"central louisiana",
                           @"houma",
                           @"lafayette",
                           @"lake charles",
                           @"monroe",
                           @"new orleans",
                           @"shreveport"];

        self.Maine = @[@"maine"];

        self.Maryland = @[@"annapolis",
                          @"baltimore",
                          @"eastern shore",
                          @"frederick",
                          @"southern maryland",
                          @"western maryland"];

        self.Massachusetts = @[@"boston",
                               @"cape cod / islands",
                               @"south coast",
                               @"western massachusetts",
                               @"worcester / central MA"];

        self.Michigan = @[@"ann arbor",
                          @"battle creek",
                          @"central michigan",
                          @"detroit metro",
                          @"flint",
                          @"grand rapids",
                          @"holland",
                          @"jackson",
                          @"kalamazoo",
                          @"lansing",
                          @"monroe",
                          @"muskegon",
                          @"northern michigan",
                          @"port huron",
                          @"saginaw-midland-baycity",
                          @"southwest michigan",
                          @"the thumb",
                          @"upper peninsula"];

        self.Mississippi = @[@"gulfport / biloxi",
                             @"hattiesburg",
                             @"jackson",
                             @"meridian",
                             @"north mississippi",
                             @"southwest MS"];

        self.Minnesota = @[@"bemidji",
                           @"brainerd",
                           @"duluth / superior",
                           @"mankato",
                           @"minneapolis / st paul",
                           @"rochester",
                           @"southwest MN",
                           @"st cloud"];

        self.Missouri = @[@"columbia / jeff city",
                          @"joplin",
                          @"kansas city",
                          @"kirksville",
                          @"lake of the ozarks",
                          @"southeast missouri",
                          @"springfield",
                          @"st joseph",
                          @"st louis"];

        self.Montana = @[@"billings",
                         @"bozeman",
                         @"butte",
                         @"great falls",
                         @"helena",
                         @"kalispell",
                         @"missoula",
                         @"eastern montana"];

        self.Nebraska = @[@"grand island",
                          @"lincoln",
                          @"north platte",
                          @"omaha / council bluffs",
                          @"scottsbluff / panhandle"];

        self.Nevada = @[@"elko",
                        @"las vegas",
                        @"reno / tahoe"];

        self.NewHampshire = @[@"New Hampshire"];

        self.NewJersey = @[@"central NJ",
                           @"jersey shore",
                           @"north jersey",
                           @"south jersey"];

        self.NewMexico = @[@"albuquerque",
                           @"clovis / portales",
                           @"farmington",
                           @"las cruces",
                           @"roswell / carlsbad",
                           @"santa fe / taos"];

        self.NewYork = @[@"albany",
                         @"binghamton",
                         @"buffalo",
                         @"catskills",
                         @"chautauqua",
                         @"elmira-corning",
                         @"finger lakes",
                         @"glens falls",
                         @"hudson valley",
                         @"ithaca",
                         @"long island",
                         @"new york city",
                         @"oneonta",
                         @"plattsburgh-adirondacks",
                         @"potsdam-canton-massena",
                         @"rochester",
                         @"syracuse",
                         @"twin tiers NY/PA",
                         @"utica-rome-oneida",
                         @"watertown"];

        self.NorthCarolina = @[@"asheville",
                               @"boone",
                               @"charlotte",
                               @"eastern NC",
                               @"fayetteville",
                               @"greensboro",
                               @"hickory / lenoir",
                               @"jacksonville",
                               @"outer banks",
                               @"raleigh / durham / CH",
                               @"wilmington",
                               @"winston-salem"];

        self.NorthDakota = @[@"bismarck",
                             @"fargo / moorhead",
                             @"grand forks",
                             @"north dakota"];

        self.Ohio = @[@"akron / canton",
                      @"ashtabula",
                      @"athens",
                      @"chillicothe",
                      @"cincinnati",
                      @"cleveland",
                      @"columbus",
                      @"dayton / springfield",
                      @"lima / findlay",
                      @"mansfield",
                      @"sandusky",
                      @"toledo",
                      @"tuscarawas co",
                      @"youngstown",
                      @"zanesville / cambridge"];

        self.Oklahoma = @[@"lawton",
                          @"northwest OK",
                          @"oklahoma city",
                          @"stillwater",
                          @"tulsa"];

        self.Oregon = @[@"bend",
                        @"corvallis/albany",
                        @"east oregon",
                        @"eugene",
                        @"klamath falls",
                        @"medford-ashland",
                        @"oregon coast",
                        @"portland",
                        @"roseburg",
                        @"salem"];

        self.Pennsylvania = @[@"altoona-johnstown",
                              @"cumberland valley",
                              @"erie",
                              @"harrisburg",
                              @"lancaster",
                              @"lehigh valley",
                              @"meadville",
                              @"philadelphia",
                              @"pittsburgh",
                              @"poconos",
                              @"reading",
                              @"scranton / wilkes-barre",
                              @"state college",
                              @"williamsport",
                              @"york"];

        self.RhodeIsland = @[@"Rhode Island"];

        self.SouthCarolina = @[@"charleston",
                               @"columbia",
                               @"florence",
                               @"greenville / upstate",
                               @"hilton head",
                               @"myrtle beach"];

        self.SouthDakota = @[@"northeast SD",
                             @"pierre / central SD",
                             @"rapid city / west SD",
                             @"sioux falls / SE SD",
                             @"south dakota"];

        self.Tennessee = @[@"chattanooga",
                           @"clarksville",
                           @"cookeville",
                           @"jackson",
                           @"knoxville",
                           @"memphis",
                           @"nashville",
                           @"tri-cities"];

        self.Texas = @[@"abilene",
                       @"amarillo",
                       @"austin",
                       @"beaumont / port arthur",
                       @"brownsville",
                       @"college station",
                       @"corpus christi",
                       @"dallas / fort worth",
                       @"deep east texas",
                       @"del rio / eagle pass",
                       @"el paso",
                       @"galveston",
                       @"houston",
                       @"killeen / temple / ft hood",
                       @"laredo",
                       @"lubbock",
                       @"mcallen / edinburg",
                       @"odessa / midland",
                       @"san angelo",
                       @"san antonio",
                       @"san marcos",
                       @"southwest TX",
                       @"texoma",
                       @"tyler / east TX",
                       @"victoria",
                       @"waco",
                       @"wichita falls"];

        self.Utah = @[@"logan",
                      @"ogden-clearfield",
                      @"provo / orem",
                      @"salt lake city",
                      @"st george"];

        self.Vermont = @[@"Vermont"];

        self.Virginia = @[@"charlottesville",
                          @"danville",
                          @"fredericksburg",
                          @"hampton roads",
                          @"harrisonburg",
                          @"lynchburg",
                          @"new river valley",
                          @"richmond",
                          @"roanoke",
                          @"southwest VA",
                          @"winchester"];

        self.Washington = @[@"bellingham",
                            @"kennewick-pasco-richland",
                            @"moses lake",
                            @"olympic peninsula",
                            @"pullman / moscow",
                            @"seattle-tacoma",
                            @"skagit / island / SJI",
                            @"spokane / coeur d'alene",
                            @"wenatchee",
                            @"yakima"];

        self.WestVirginia = @[@"charleston",
                              @"eastern panhandle",
                              @"huntington-ashland",
                              @"morgantown",
                              @"northern panhandle",
                              @"parkersburg-marietta",
                              @"southern WV",
                              @"west virginia (old)"];

        self.Wisconsin = @[@"appleton-oshkosh-FDL",
                           @"eau claire",
                           @"green bay",
                           @"janesville",
                           @"kenosha-racine",
                           @"la crosse",
                           @"madison",
                           @"milwaukee",
                           @"northern WI",
                           @"sheboygan",
                           @"wausau"];

        self.Wyoming = @[@"Wyoming"];

NSMutableDictionary *dic1=[[NSMutableDictionary alloc]init];
        [dic1 setValue:@"Alabama" forKey:@"state"];
        [dic1 setValue:self.Alabama forKey:@"cities"];


        NSMutableDictionary *dic2=[[NSMutableDictionary alloc]init];
        [dic2 setValue:@"Alaska" forKey:@"state"];
        [dic2 setValue:self.Alaska forKey:@"cities"];

        NSMutableDictionary *dic3=[[NSMutableDictionary alloc]init];
        [dic3 setValue:@"Arizona" forKey:@"states"];
        [dic3 setValue:self.Arizona forKey:@"cities"];


        NSMutableDictionary *dic4=[[NSMutableDictionary alloc]init];
        [dic4 setValue:@"Arkansas" forKey:@"states"];
        [dic4 setValue:self.Arkansas forKey:@"cities"];

        NSMutableDictionary *dic5=[[NSMutableDictionary alloc]init];
        [dic5 setValue:@"California" forKey:@"states"];
        [dic5 setValue:self.California forKey:@"cities"];


        NSMutableDictionary *dic6=[[NSMutableDictionary alloc]init];
        [dic6 setValue:@"Colorado" forKey:@"states"];
        [dic6 setValue:self.Colorado forKey:@"cities"];

        NSMutableDictionary *dic7=[[NSMutableDictionary alloc]init];
        [dic7 setValue:@"Connecticut" forKey:@"state"];
        [dic7 setValue:self.Connecticut forKey:@"cities"];


        NSMutableDictionary *dic8=[[NSMutableDictionary alloc]init];
        [dic8 setValue:@"Delaware" forKey:@"state"];
        [dic8 setValue:self.Delaware forKey:@"cities"];

        NSMutableDictionary *dic9=[[NSMutableDictionary alloc]init];
        [dic9 setValue:@"Florida" forKey:@"states"];
        [dic9 setValue:self.Florida forKey:@"cities"];


        NSMutableDictionary *dic10=[[NSMutableDictionary alloc]init];
        [dic10 setValue:@"Georgia" forKey:@"states"];
        [dic10 setValue:self.Georgia forKey:@"cities"];

        NSMutableDictionary *dic11=[[NSMutableDictionary alloc]init];
        [dic11 setValue:@"Hawaii" forKey:@"states"];
        [dic11 setValue:self.Hawaii forKey:@"cities"];


        NSMutableDictionary *dic12=[[NSMutableDictionary alloc]init];
        [dic12 setValue:@"Idaho" forKey:@"states"];
        [dic12 setValue:self.Idaho forKey:@"cities"];

        NSMutableDictionary *dic13=[[NSMutableDictionary alloc]init];
        [dic13 setValue:@"Illinois" forKey:@"state"];
        [dic13 setValue:self.Illinois forKey:@"cities"];


        NSMutableDictionary *dic14=[[NSMutableDictionary alloc]init];
        [dic14 setValue:@"Indiana" forKey:@"state"];
        [dic14 setValue:self.Indiana forKey:@"cities"];

        NSMutableDictionary *dic15=[[NSMutableDictionary alloc]init];
        [dic15 setValue:@"Iowa" forKey:@"states"];
        [dic15 setValue:self.Iowa forKey:@"cities"];


        NSMutableDictionary *dic16=[[NSMutableDictionary alloc]init];
        [dic16 setValue:@"Kansas" forKey:@"states"];
        [dic16 setValue:self.Kansas forKey:@"cities"];

        NSMutableDictionary *dic17=[[NSMutableDictionary alloc]init];
        [dic17 setValue:@"Kentucky" forKey:@"states"];
        [dic17 setValue:self.Kentucky forKey:@"cities"];


        NSMutableDictionary *dic18=[[NSMutableDictionary alloc]init];
        [dic18 setValue:@"Louisiana" forKey:@"states"];
        [dic18 setValue:self.Louisiana forKey:@"cities"];

        NSMutableDictionary *dic19=[[NSMutableDictionary alloc]init];
        [dic19 setValue:@"Maine" forKey:@"state"];
        [dic19 setValue:self.Maine forKey:@"cities"];


        NSMutableDictionary *dic20=[[NSMutableDictionary alloc]init];
        [dic20 setValue:@"Maryland" forKey:@"state"];
        [dic20 setValue:self.Maryland forKey:@"cities"];

        NSMutableDictionary *dic21=[[NSMutableDictionary alloc]init];
        [dic21 setValue:@"Massachusetts" forKey:@"states"];
        [dic21 setValue:self.Massachusetts forKey:@"cities"];


        NSMutableDictionary *dic22=[[NSMutableDictionary alloc]init];
        [dic22 setValue:@"Michigan" forKey:@"states"];
        [dic22 setValue:self.Michigan forKey:@"cities"];

        NSMutableDictionary *dic23=[[NSMutableDictionary alloc]init];
        [dic23 setValue:@"Minnesota" forKey:@"states"];
        [dic23 setValue:self.Minnesota forKey:@"cities"];


        NSMutableDictionary *dic24=[[NSMutableDictionary alloc]init];
        [dic24 setValue:@"Mississippi" forKey:@"states"];
        [dic24 setValue:self.Mississippi forKey:@"cities"];

        NSMutableDictionary *dic25=[[NSMutableDictionary alloc]init];
        [dic25 setValue:@"Missouri" forKey:@"states"];
        [dic25 setValue:self.Missouri forKey:@"cities"];


        NSMutableDictionary *dic26=[[NSMutableDictionary alloc]init];
        [dic26 setValue:@"Montana" forKey:@"states"];
        [dic26 setValue:self.Montana forKey:@"cities"];

        NSMutableDictionary *dic27=[[NSMutableDictionary alloc]init];
        [dic27 setValue:@"Nebraska" forKey:@"states"];
        [dic27 setValue:self.Nebraska forKey:@"cities"];


        NSMutableDictionary *dic28=[[NSMutableDictionary alloc]init];
        [dic28 setValue:@"Nevada" forKey:@"states"];
        [dic28 setValue:self.Nevada forKey:@"cities"];

1 个答案:

答案 0 :(得分:0)

我建议为每个州创建一个类,其中包含属性的NSString和包含所有相应城市的数组。然后,将状态对象存储在字典中,状态名称为键。