xmartlabs / XLForm

XLForm is the most flexible and powerful iOS library to create dynamic table-view forms. Fully compatible with Swift & Obj-C.
MIT License
5.77k stars 953 forks source link

How to store and show different values in row? #584

Closed AlexEdunov closed 9 years ago

AlexEdunov commented 9 years ago

For example, I have row in my form:

#pragma mark - Lazy loading

- (XLFormRowDescriptor *)rowCountry
{
    if (!_rowCountry) {
        NSString *tag = @"country";
        NSString *type = XLFormRowDescriptorTypeSelectorPush;
        NSString *title = NSLocalizedString(@"Country", nil);

        _rowCountry = [XLFormRowDescriptor formRowDescriptorWithTag:tag rowType:type title:title];
        _rowCountry.action.viewControllerClass = [BOCountryListViewController class];
    }

    _rowCountry.value = self.formModel.countryISO;

    return _rowCountry;
}

In BOCountryListViewController class I have cell selection handler:

#pragma mark - UITableViewDelegate  

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOCountry *country = [self.allItems objectAtIndex:indexPath.row];
    self.rowDescriptor.value = country.code;

    [self.navigationController popViewControllerAnimated:YES];
}

There is my Country class interface:

@interface BOCountry : MTLModel

@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *name;

@end

How I can show name of the country in form row and store code in formValues?

tommypeps commented 9 years ago
[
          {
            "prompt" : "Afghanistan",
            "value" : "AF"
          },
          {
            "prompt" : "Albania",
            "value" : "AL"
          },
          {
            "prompt" : "Algeria",
            "value" : "DZ"
          },
          {
            "prompt" : "American Samoa",
            "value" : "DS"
          },
          {
            "prompt" : "Andorra",
            "value" : "AD"
          },
          {
            "prompt" : "Angola",
            "value" : "AO"
          },
          {
            "prompt" : "Anguilla",
            "value" : "AI"
          },
          {
            "prompt" : "Antarctica",
            "value" : "AQ"
          },
          {
            "prompt" : "Antigua And/or Barbuda",
            "value" : "AG"
          },
          {
            "prompt" : "Argentina",
            "value" : "AR"
          },
          {
            "prompt" : "Armenia",
            "value" : "AM"
          },
          {
            "prompt" : "Aruba",
            "value" : "AW"
          },
          {
            "prompt" : "Australia",
            "value" : "AU"
          },
          {
            "prompt" : "Austria",
            "value" : "AT"
          },
          {
            "prompt" : "Azerbaijan",
            "value" : "AZ"
          },
          {
            "prompt" : "Bahamas",
            "value" : "BS"
          },
          {
            "prompt" : "Bahrain",
            "value" : "BH"
          },
          {
            "prompt" : "Bangladesh",
            "value" : "BD"
          },
          {
            "prompt" : "Barbados",
            "value" : "BB"
          },
          {
            "prompt" : "Belarus",
            "value" : "BY"
          },
          {
            "prompt" : "Belgium",
            "value" : "BE"
          },
          {
            "prompt" : "Belize",
            "value" : "BZ"
          },
          {
            "prompt" : "Benin",
            "value" : "BJ"
          },
          {
            "prompt" : "Bermuda",
            "value" : "BM"
          },
          {
            "prompt" : "Bhutan",
            "value" : "BT"
          },
          {
            "prompt" : "Bolivia",
            "value" : "BO"
          },
          {
            "prompt" : "Bosnia And Herzegovina",
            "value" : "BA"
          },
          {
            "prompt" : "Botswana",
            "value" : "BW"
          },
          {
            "prompt" : "Bouvet Island",
            "value" : "BV"
          },
          {
            "prompt" : "Brazil",
            "value" : "BR"
          },
          {
            "prompt" : "British Lndian Ocean Territory",
            "value" : "IO"
          },
          {
            "prompt" : "Brunei Darussalam",
            "value" : "BN"
          },
          {
            "prompt" : "Bulgaria",
            "value" : "BG"
          },
          {
            "prompt" : "Burkina Faso",
            "value" : "BF"
          },
          {
            "prompt" : "Burundi",
            "value" : "BI"
          },
          {
            "prompt" : "Cambodia",
            "value" : "KH"
          },
          {
            "prompt" : "Cameroon",
            "value" : "CM"
          },
          {
            "prompt" : "Canada",
            "value" : "CA"
          },
          {
            "prompt" : "Cape Verde",
            "value" : "CV"
          },
          {
            "prompt" : "Cayman Islands",
            "value" : "KY"
          },
          {
            "prompt" : "Central African Republic",
            "value" : "CF"
          },
          {
            "prompt" : "Central Europ",
            "value" : "CE"
          },
          {
            "prompt" : "Chad",
            "value" : "TD"
          },
          {
            "prompt" : "Chile",
            "value" : "CL"
          },
          {
            "prompt" : "China",
            "value" : "CN"
          },
          {
            "prompt" : "Christmas Island",
            "value" : "CX"
          },
          {
            "prompt" : "Cocos (Keeling) Islands",
            "value" : "CC"
          },
          {
            "prompt" : "Colombia",
            "value" : "CO"
          },
          {
            "prompt" : "Comoros",
            "value" : "KM"
          },
          {
            "prompt" : "Congo",
            "value" : "CG"
          },
          {
            "prompt" : "Cook Islands",
            "value" : "CK"
          },
          {
            "prompt" : "Costa Rica",
            "value" : "CR"
          },
          {
            "prompt" : "Croatia (Hrvatska)",
            "value" : "HR"
          },
          {
            "prompt" : "Cuba",
            "value" : "CU"
          },
          {
            "prompt" : "Cyprus",
            "value" : "CY"
          },
          {
            "prompt" : "Czech Republic",
            "value" : "CZ"
          },
          {
            "prompt" : "Denmark",
            "value" : "DK"
          },
          {
            "prompt" : "Djibouti",
            "value" : "DJ"
          },
          {
            "prompt" : "Dominica",
            "value" : "DM"
          },
          {
            "prompt" : "Dominican Republic",
            "value" : "DO"
          },
          {
            "prompt" : "East Timor",
            "value" : "TL"
          },
          {
            "prompt" : "East Timor",
            "value" : "TP"
          },
          {
            "prompt" : "Ecudaor",
            "value" : "EC"
          },
          {
            "prompt" : "Egypt",
            "value" : "EG"
          },
          {
            "prompt" : "El Salvador",
            "value" : "SV"
          },
          {
            "prompt" : "Equatorial Guinea",
            "value" : "GQ"
          },
          {
            "prompt" : "Eritrea",
            "value" : "ER"
          },
          {
            "prompt" : "Estonia",
            "value" : "EE"
          },
          {
            "prompt" : "Ethiopia",
            "value" : "ET"
          },
          {
            "prompt" : "Falkland Islands (Malvinas)",
            "value" : "FK"
          },
          {
            "prompt" : "Faroe Islands",
            "value" : "FO"
          },
          {
            "prompt" : "Fiji",
            "value" : "FJ"
          },
          {
            "prompt" : "Finland",
            "value" : "FI"
          },
          {
            "prompt" : "France",
            "value" : "FR"
          },
          {
            "prompt" : "France, Metropolitan",
            "value" : "FX"
          },
          {
            "prompt" : "French Guiana",
            "value" : "GF"
          },
          {
            "prompt" : "French Polynesia",
            "value" : "PF"
          },
          {
            "prompt" : "French Southern Territories",
            "value" : "TF"
          },
          {
            "prompt" : "Gabon",
            "value" : "GA"
          },
          {
            "prompt" : "Gambia",
            "value" : "GM"
          },
          {
            "prompt" : "Georgia",
            "value" : "GE"
          },
          {
            "prompt" : "Germany",
            "value" : "DE"
          },
          {
            "prompt" : "Ghana",
            "value" : "GH"
          },
          {
            "prompt" : "Gibraltar",
            "value" : "GI"
          },
          {
            "prompt" : "Greece",
            "value" : "GR"
          },
          {
            "prompt" : "Greenland",
            "value" : "GL"
          },
          {
            "prompt" : "Grenada",
            "value" : "GD"
          },
          {
            "prompt" : "Guadeloupe",
            "value" : "GP"
          },
          {
            "prompt" : "Guam",
            "value" : "GU"
          },
          {
            "prompt" : "Guatemala",
            "value" : "GT"
          },
          {
            "prompt" : "Guinea",
            "value" : "GN"
          },
          {
            "prompt" : "Guinea-Bissau",
            "value" : "GW"
          },
          {
            "prompt" : "Guyana",
            "value" : "GY"
          },
          {
            "prompt" : "Haiti",
            "value" : "HT"
          },
          {
            "prompt" : "Heard And Mc Donald Islands",
            "value" : "HM"
          },
          {
            "prompt" : "Honduras",
            "value" : "HN"
          },
          {
            "prompt" : "Hong Kong",
            "value" : "HK"
          },
          {
            "prompt" : "Hungary",
            "value" : "HU"
          },
          {
            "prompt" : "Iceland",
            "value" : "IS"
          },
          {
            "prompt" : "India",
            "value" : "IN"
          },
          {
            "prompt" : "Indonesia",
            "value" : "ID"
          },
          {
            "prompt" : "Iran (Islamic Republic Of)",
            "value" : "IR"
          },
          {
            "prompt" : "Iraq",
            "value" : "IQ"
          },
          {
            "prompt" : "Ireland",
            "value" : "IE"
          },
          {
            "prompt" : "Israel",
            "value" : "IL"
          },
          {
            "prompt" : "Italy",
            "value" : "IT"
          },
          {
            "prompt" : "Ivory Coast",
            "value" : "CI"
          },
          {
            "prompt" : "Jamaica",
            "value" : "JM"
          },
          {
            "prompt" : "Japan",
            "value" : "JP"
          },
          {
            "prompt" : "Jordan",
            "value" : "JO"
          },
          {
            "prompt" : "Kazakhstan",
            "value" : "KZ"
          },
          {
            "prompt" : "Kenya",
            "value" : "KE"
          },
          {
            "prompt" : "Kiribati",
            "value" : "KI"
          },
          {
            "prompt" : "Korea, Democratic People's Republic Of",
            "value" : "KP"
          },
          {
            "prompt" : "Korea, Republic Of",
            "value" : "KR"
          },
          {
            "prompt" : "Kosovo",
            "value" : "_1"
          },
          {
            "prompt" : "Kuwait",
            "value" : "KW"
          },
          {
            "prompt" : "Kyrgyzstan",
            "value" : "KG"
          },
          {
            "prompt" : "Lao People's Democratic Republic",
            "value" : "LA"
          },
          {
            "prompt" : "Latvia",
            "value" : "LV"
          },
          {
            "prompt" : "Lebanon",
            "value" : "LB"
          },
          {
            "prompt" : "Lesotho",
            "value" : "LS"
          },
          {
            "prompt" : "Liberia",
            "value" : "LR"
          },
          {
            "prompt" : "Libyan Arab Jamahiriya",
            "value" : "LY"
          },
          {
            "prompt" : "Liechtenstein",
            "value" : "LI"
          },
          {
            "prompt" : "Lithuania",
            "value" : "LT"
          },
          {
            "prompt" : "Luxembourg",
            "value" : "LU"
          },
          {
            "prompt" : "Macau",
            "value" : "MO"
          },
          {
            "prompt" : "Macedonia",
            "value" : "MK"
          },
          {
            "prompt" : "Madagascar",
            "value" : "MG"
          },
          {
            "prompt" : "Malawi",
            "value" : "MW"
          },
          {
            "prompt" : "Malaysia",
            "value" : "MY"
          },
          {
            "prompt" : "Maldives",
            "value" : "MV"
          },
          {
            "prompt" : "Mali",
            "value" : "ML"
          },
          {
            "prompt" : "Malta",
            "value" : "MT"
          },
          {
            "prompt" : "Marshall Islands",
            "value" : "MH"
          },
          {
            "prompt" : "Martinique",
            "value" : "MQ"
          },
          {
            "prompt" : "Mauritania",
            "value" : "MR"
          },
          {
            "prompt" : "Mauritius",
            "value" : "MU"
          },
          {
            "prompt" : "Mayotte",
            "value" : "TY"
          },
          {
            "prompt" : "Mexico",
            "value" : "MX"
          },
          {
            "prompt" : "Micronesia, Federated States Of",
            "value" : "FM"
          },
          {
            "prompt" : "Moldova, Republic Of",
            "value" : "MD"
          },
          {
            "prompt" : "Monaco",
            "value" : "MC"
          },
          {
            "prompt" : "Mongolia",
            "value" : "MN"
          },
          {
            "prompt" : "Montenegro",
            "value" : "ME"
          },
          {
            "prompt" : "Montserrat",
            "value" : "MS"
          },
          {
            "prompt" : "Morocco",
            "value" : "MA"
          },
          {
            "prompt" : "Mozambique",
            "value" : "MZ"
          },
          {
            "prompt" : "Myanmar",
            "value" : "MM"
          },
          {
            "prompt" : "Namibia",
            "value" : "NA"
          },
          {
            "prompt" : "Nauru",
            "value" : "NR"
          },
          {
            "prompt" : "Nepal",
            "value" : "NP"
          },
          {
            "prompt" : "Netherlands",
            "value" : "NL"
          },
          {
            "prompt" : "Netherlands Antilles",
            "value" : "AN"
          },
          {
            "prompt" : "New Caledonia",
            "value" : "NC"
          },
          {
            "prompt" : "New Zealand",
            "value" : "NZ"
          },
          {
            "prompt" : "Nicaragua",
            "value" : "NI"
          },
          {
            "prompt" : "Niger",
            "value" : "NE"
          },
          {
            "prompt" : "Nigeria",
            "value" : "NG"
          },
          {
            "prompt" : "Niue",
            "value" : "NU"
          },
          {
            "prompt" : "Norfork Island",
            "value" : "NF"
          },
          {
            "prompt" : "Northern Mariana Islands",
            "value" : "MP"
          },
          {
            "prompt" : "Norway",
            "value" : "NO"
          },
          {
            "prompt" : "Oman",
            "value" : "OM"
          },
          {
            "prompt" : "Pakistan",
            "value" : "PK"
          },
          {
            "prompt" : "Palau",
            "value" : "PW"
          },
          {
            "prompt" : "Panama",
            "value" : "PA"
          },
          {
            "prompt" : "Papua New Guinea",
            "value" : "PG"
          },
          {
            "prompt" : "Paraguay",
            "value" : "PY"
          },
          {
            "prompt" : "Peru",
            "value" : "PE"
          },
          {
            "prompt" : "Philippines",
            "value" : "PH"
          },
          {
            "prompt" : "Pitcairn",
            "value" : "PN"
          },
          {
            "prompt" : "Poland",
            "value" : "PL"
          },
          {
            "prompt" : "Portugal",
            "value" : "PT"
          },
          {
            "prompt" : "Puerto Rico",
            "value" : "PR"
          },
          {
            "prompt" : "Qatar",
            "value" : "QA"
          },
          {
            "prompt" : "Republic Democratic Of Congo",
            "value" : "CD"
          },
          {
            "prompt" : "Republic Of Serbia",
            "value" : "RS"
          },
          {
            "prompt" : "Reunion",
            "value" : "RE"
          },
          {
            "prompt" : "Romania",
            "value" : "RO"
          },
          {
            "prompt" : "Russian Federation",
            "value" : "RU"
          },
          {
            "prompt" : "Rwanda",
            "value" : "RW"
          },
          {
            "prompt" : "Saint Kitts And Nevis",
            "value" : "KN"
          },
          {
            "prompt" : "Saint Lucia",
            "value" : "LC"
          },
          {
            "prompt" : "Saint Vincent And The Grenadines",
            "value" : "VC"
          },
          {
            "prompt" : "Samoa",
            "value" : "WS"
          },
          {
            "prompt" : "San Marino",
            "value" : "SM"
          },
          {
            "prompt" : "Sao Tome And Principe",
            "value" : "ST"
          },
          {
            "prompt" : "Saudi Arabia",
            "value" : "SA"
          },
          {
            "prompt" : "Senegal",
            "value" : "SN"
          },
          {
            "prompt" : "Seychelles",
            "value" : "SC"
          },
          {
            "prompt" : "Sierra Leone",
            "value" : "SL"
          },
          {
            "prompt" : "Singapore",
            "value" : "SG"
          },
          {
            "prompt" : "Slovakia",
            "value" : "SK"
          },
          {
            "prompt" : "Slovenia",
            "value" : "SI"
          },
          {
            "prompt" : "Solomon Islands",
            "value" : "SB"
          },
          {
            "prompt" : "Somalia",
            "value" : "SO"
          },
          {
            "prompt" : "Somaliland",
            "value" : "_3"
          },
          {
            "prompt" : "South Africa",
            "value" : "ZA"
          },
          {
            "prompt" : "South Georgia South Sandwich Islands",
            "value" : "GS"
          },
          {
            "prompt" : "South Sudan",
            "value" : "SS"
          },
          {
            "prompt" : "Spain",
            "value" : "ES"
          },
          {
            "prompt" : "Sri Lanka",
            "value" : "LK"
          },
          {
            "prompt" : "St. Helena",
            "value" : "SH"
          },
          {
            "prompt" : "St. Pierre And Miquelon",
            "value" : "PM"
          },
          {
            "prompt" : "Sudan",
            "value" : "SD"
          },
          {
            "prompt" : "Suriname",
            "value" : "SR"
          },
          {
            "prompt" : "Svalbarn And Jan Mayen Islands",
            "value" : "SJ"
          },
          {
            "prompt" : "Swaziland",
            "value" : "SZ"
          },
          {
            "prompt" : "Sweden",
            "value" : "SE"
          },
          {
            "prompt" : "Switzerland",
            "value" : "CH"
          },
          {
            "prompt" : "Syrian Arab Republic",
            "value" : "SY"
          },
          {
            "prompt" : "Taiwan",
            "value" : "TW"
          },
          {
            "prompt" : "Tajikistan",
            "value" : "TJ"
          },
          {
            "prompt" : "Tanzania, United Republic Of",
            "value" : "TZ"
          },
          {
            "prompt" : "Thailand",
            "value" : "TH"
          },
          {
            "prompt" : "Togo",
            "value" : "TG"
          },
          {
            "prompt" : "Tokelau",
            "value" : "TK"
          },
          {
            "prompt" : "Tonga",
            "value" : "TO"
          },
          {
            "prompt" : "Trinidad And Tobago",
            "value" : "TT"
          },
          {
            "prompt" : "Tunisia",
            "value" : "TN"
          },
          {
            "prompt" : "Turkey",
            "value" : "TR"
          },
          {
            "prompt" : "Turkmenistan",
            "value" : "TM"
          },
          {
            "prompt" : "Turks And Caicos Islands",
            "value" : "TC"
          },
          {
            "prompt" : "Tuvalu",
            "value" : "TV"
          },
          {
            "prompt" : "Uganda",
            "value" : "UG"
          },
          {
            "prompt" : "Ukraine",
            "value" : "UA"
          },
          {
            "prompt" : "United Arab Emirates",
            "value" : "AE"
          },
          {
            "prompt" : "United Kingdom",
            "value" : "GB"
          },
          {
            "prompt" : "United States",
            "value" : "US"
          },
          {
            "prompt" : "United States Minor Outlying Islands",
            "value" : "UM"
          },
          {
            "prompt" : "Uruguay",
            "value" : "UY"
          },
          {
            "prompt" : "Uzbekistan",
            "value" : "UZ"
          },
          {
            "prompt" : "Vanuatu",
            "value" : "VU"
          },
          {
            "prompt" : "Vatican City State",
            "value" : "VA"
          },
          {
            "prompt" : "Venezuela",
            "value" : "VE"
          },
          {
            "prompt" : "Vietnam",
            "value" : "VN"
          },
          {
            "prompt" : "Virgin Islands (U.S.)",
            "value" : "VI"
          },
          {
            "prompt" : "Virigan Islands (British)",
            "value" : "VG"
          },
          {
            "prompt" : "Wallis And Futuna Islands",
            "value" : "WF"
          },
          {
            "prompt" : "West Bank",
            "value" : "PS"
          },
          {
            "prompt" : "Western Sahara",
            "value" : "_2"
          },
          {
            "prompt" : "Western Sahara",
            "value" : "EH"
          },
          {
            "prompt" : "Yemen",
            "value" : "YE"
          },
          {
            "prompt" : "Yugoslavia",
            "value" : "YU"
          },
          {
            "prompt" : "Zaire",
            "value" : "ZR"
          },
          {
            "prompt" : "Zambia",
            "value" : "ZM"
          },
          {
            "prompt" : "Zimbabwe",
            "value" : "ZW"
          }
        ]

for this struct listOfContry contains above information

NSArray * listOfContry;
        [listOfContry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            XLFormOptionsObject *option =  [XLFormOptionsObject formOptionsObjectWithValue:obj[@"value"]
                                                                               displayText:obj[@"prompt"]];
            row.selectorOptions = [row.selectorOptions arrayByAddingObject:option];
        }];
AlexEdunov commented 9 years ago

That's exactly what I need. Thank you!

My result code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOCountry *country = [self.allItems objectAtIndex:indexPath.row];

    XLFormOptionsObject *option = [XLFormOptionsObject formOptionsObjectWithValue:country.code
                                                                    displayText:country.name];
    self.rowDescriptor.value = option;

    [self.navigationController popViewControllerAnimated:YES];
}
KinmoCode commented 6 years ago

@AlexEdunov Can you share this part of the code?