iOS: Programmatically add different tableviewcells - objective-c

I would like to know what the best approach would be for creating and adding rows to a uitableview based on data from an API. The data returned from the API would look like the below. Some fields will need to be standard text, some would need to by password text fields, some number fields and some would be switches and date picker fields.
I'm guessing I need to add a field one of each cells to my UITableView in order to have the cell base. Then depending on the cell I am adding at the time I will retrieve that cell and add it to the table.
How would I keep track of the data? When the user submits the info how will I know which field is which?
The fields returned by the API will depend on which settings they have enabled within our site. And the order will vary depending on what sort_order they have set.
"data": [
{
"field_name": "location_id",
"display_name": "Home Location",
"page_cust": "O",
"sort_order": "10"
},
{
"field_name": "first_name",
"display_name": "First Name1",
"page_cust": "R",
"sort_order": "20"
},
{
"field_name": "middle_name",
"display_name": "Middle Name",
"page_cust": "O",
"sort_order": "25"
},
{
"field_name": "last_name",
"display_name": "Last Name1",
"page_cust": "R",
"sort_order": "30"
},
{
"field_name": "address1",
"display_name": "Address",
"page_cust": "O",
"sort_order": "40"
},
{
"field_name": "address2",
"display_name": "Address 2",
"page_cust": "O",
"sort_order": "45"
},
{
"field_name": "city",
"display_name": "City",
"page_cust": "O",
"sort_order": "50"
},
{
"field_name": "state",
"display_name": "State",
"page_cust": "O",
"sort_order": "60"
},
{
"field_name": "zip",
"display_name": "Zip Code",
"page_cust": "O",
"sort_order": "70"
},
{
"field_name": "day_phone",
"display_name": "Daytime Phone",
"page_cust": "O",
"sort_order": "80"
},
{
"field_name": "night_phone",
"display_name": "Evening Phone",
"page_cust": "O",
"sort_order": "90"
},
{
"field_name": "cell_phone",
"display_name": "Cell Phone",
"page_cust": "O",
"sort_order": "100"
},
{
"field_name": "email",
"display_name": "eMail",
"page_cust": "O",
"sort_order": "110"
},
{
"field_name": "login",
"display_name": "Login",
"page_cust": "O",
"sort_order": "120"
},
{
"field_name": "password",
"display_name": "Password",
"page_cust": "O",
"sort_order": "130"
},
{
"field_name": "lead_id",
"display_name": "Heard Via?",
"page_cust": "O",
"sort_order": "140"
},
{
"field_name": "contact_okay",
"display_name": "Contact Okay",
"page_cust": "O",
"sort_order": "170"
},
{
"field_name": "call_okay",
"display_name": "Call Okay",
"page_cust": "O",
"sort_order": "180"
},
{
"field_name": "email_okay",
"display_name": "E-mail Okay",
"page_cust": "O",
"sort_order": "190"
},
{
"field_name": "mail_okay",
"display_name": "Mail Okay",
"page_cust": "O",
"sort_order": "200"
},
{
"field_name": "payment_type_id",
"display_name": "Payment Method",
"page_cust": "O",
"sort_order": "210"
},
{
"field_name": "employer",
"display_name": "Company",
"page_cust": "O",
"sort_order": "260"
},
{
"field_name": "occupation",
"display_name": "Occupation",
"page_cust": "O",
"sort_order": "270"
},
{
"field_name": "birth_date",
"display_name": "Birth Date",
"page_cust": "O",
"sort_order": "280"
},
{
"field_name": "gender",
"display_name": "Gender",
"page_cust": "O",
"sort_order": "290"
},
{
"field_name": "status_id",
"display_name": "Status",
"page_cust": "O",
"sort_order": "340"
},
{
"field_name": "allow_login",
"display_name": "Allow to Log In",
"page_cust": "O",
"sort_order": "350"
},
{
"field_name": "customer_type_id",
"display_name": "Customer Type",
"page_cust": "O",
"sort_order": "360"
},
{
"field_name": "rep_id",
"display_name": "Assigned To",
"page_cust": "O",
"sort_order": "370"
},
{
"field_name": "account",
"display_name": "Account Number",
"page_cust": "O",
"sort_order": "380"
},
{
"field_name": "needs",
"display_name": "Special Needs",
"page_cust": "O",
"sort_order": "390"
}
]

You are consuming a web-service. Based on user setting you get back a JSON response in some particular order and composed by particular fields.
You already parsed the JSON, and you get a dictionary with one key ("data") for an array of dictionaries, right ?
The simplest approach is to store these dictionaries in a mutable array, NSMutableArray.
You know which cell is which object, because the datasource will be asked for the cell at a particular row number. Cell for row 0 will display fields of the first dictionary in the array, and so on.
NSDictionary *dictAtRow0 = [mySourceArray objectAtIndex:0];
If you wonder how to know if a particular field is present in the dictionary, test the presence of the corresponding key in the dictionary, something like this :
if([[dictAtRow0 allKeys] containsObject:#"field_name"]) {
// here you know the dictionary has an entry for the key "field_name"
}
It is also possible to reorder the array content based on the value for the "sort_order" key. Although the web-service seems to respect that order, you might want to do it programmatically. What I recommend is to do it before populating the source array :
// you parsed JSON and get the dictionary
NSArray *sortedArray = [[mainDict objectForKey:#"data"] sortedArrayUsingComparator: ^(id a, id b) {
NSNumber *first = [NSNumber numberWithInteger:[[a objectForKey:#"sort_order"] integerValue]];
NSNumber *second = [NSNumber numberWithInteger:[[b objectForKey:#"sort_order"] integerValue]];
return [first compare:second];
}];
mySourceArray = [[NSMutableArray alloc] initWithArray:sortedArray];
I didn't test that code, the comparator is maybe buggy but you got the idea.

Related

Mongoose alternative to SQL SELECT field, COUNT(field) FROM db

Is there a mongoose function in order to find a field value and the amount of times it exists in the document.
For example:
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "400000"
},
I need the function to return something like:
bin: 411111, count: 3
bin: 400000, count: 1
You can use the following aggregation pipeline to achieve exactly what you want:
.aggregate([
{
$group: {
_id: "$Info.bin",
count: {
$sum: 1
}
}
}
])

Update date format in jsonb object in postgreSQL

Postgres table have one column as JSONB with below format
{
"steps": [
{
"step": "Building",
"status": "Complete",
"end_date": "03/08/2018",
"start_date": "03/08/2018"
},
{
"step": "Underground Mechanical",
"status": "Not Applicable",
"end_date": "04/25/2018",
"start_date": ""
},
{
"step": "Close Mechanical Permit",
"status": "Complete",
"end_date": "04/25/2018",
"start_date": ""
}
],
"people": [
{
"name": "Energy Resource Center",
"role": "Contractor",
"phone": "(000) 444-4447"
},
{
"name": "XXX YYY",
"role": "Owner",
"phone": ""
}
],
"status": "Closed",
"address": "XXX str",
"sub_type": "Residential",
"issue_date": "03/08/2018",
"description": ""
}
steps can have n number of objects.
I need to update end_date , start_date and issue_date to epoch date format. Please anyone can help with script.

How to map an array within an array and achieve the following output?

In the given input,
{
"editable": true,
"sections": [
{
"title": "Identification",
"calingaKey": "",
"content": [
[{
"name": "Classification",
"text": "Product",
"url": "",
"info": ""
},
{
"name": "Product Number",
"text": "####1234",
"url": "",
"info": ""
}]
]
},
{
"title": "Position and Contact",
"calingaKey": "",
"content": [
[{
"name": "Manufacturer",
"text": "Value of Manufacturer",
"url": "",
"info": ""
},
{
"name": "Hardware Version",
"text": "####1234",
"url": "",
"info": ""
}]
]
}
]
}
"content" is an array of array of objects. Basically, the "name" field has to be replaced by values stored in their corresponding keys in the "calinga" variable.
I could do it for the "title" field, but each "name" field should also be replaced by it's name in the variable.
%dw 2.0
output application/json
var calinga = {
"Identification": "Identifikation",
"Position and Contact": "Positions und Contacts",
"Classification": "Classifikation",
"Product Number": "Produkt Number",
"Manufacturer": "Manufakturer",
"Hardware Version": "Hware Vsion"
}
---
{
"editable": payload.editable,
"sections": payload.sections map(item01, index01)->{
"title": calinga[item01.title],
"content": item01.content map(item02)->(item02)
}
}
How Can I achieve the following output?
{
"editable": true,
"sections": [
{
"title": "Identifikation",
"calingaKey": "",
"content": [
[{
"name": "Classifikation",
"text": "Product",
"url": "",
"info": ""
},
{
"name": "Produkt Number",
"text": "####1234",
"url": "",
"info": ""
}]
]
},
{
"title": "Positions und Contacts",
"calingaKey": "",
"content": [
[{
"name": "Manufakturer",
"text": "Value of Manufacturer",
"url": "",
"info": ""
},
{
"name": "Hware Vsion",
"text": "####1234",
"url": "",
"info": ""
}]
]
}
]
}
You can use mapObject() once you descend from the last nested array into objects. Then the trick is to use the value of calinga but if it null because the key is not present then use the original value as the default: item03 mapObject {($$):calinga[$] default $}.
Example:
%dw 2.0
output application/json
var calinga = {
"Identification": "Identifikation",
"Position and Contact": "Positions und Contacts",
"Classification": "Classifikation",
"Product Number": "Produkt Number",
"Manufacturer": "Manufakturer",
"Hardware Version": "Hware Vsion"
}
---
{
"editable": payload.editable,
"sections": payload.sections map(item01, index01)->{
"title": calinga[item01.title],
"content": item01.content map(item02)->(item02 map(item03)-> item03 mapObject {($$):calinga[$] default $})
}
}

Scenario Outline in Karate for multi-line JSON

How to use scenario outline to iterate for a JSON which is more than single cell
https://github.com/intuit/karate#the-cucumber-way
Data.json
[{
"address": {
"addressLine1": "ttes",
"addressLine2": "Test",
"addressLine3": "Test",
"addressType": "business",
"city": "TEST",
"company": "TEST",
"country": "TEST",
"state": "TEST",
"postalCode": "XXXX"
},
"name": {
"firstName": "TEST",
"lastName": "TEST",
"middleInit": "T",
"title": "Mr."
},
"phone": {
"phoneExtension": "1234",
"phoneNumber": "999999999",
"phoneType": "mobile"
},
"email": {
"emailAddress": "TEST#TEST.com"
}
},
{
"address": {
"addressLine1": "ttes1",
"addressLine2": "Test1",
"addressLine3": "Test1",
"addressType": "business",
"city": "TEST1",
"company": "TEST1",
"country": "TEST1",
"state": "TEST1",
"postalCode": "XXXX"
},
"name": {
"firstName": "TEST1",
"lastName": "TEST1",
"middleInit": "T",
"title": "Mr."
},
"phone": {
"phoneExtension": "1234",
"phoneNumber": "999999999",
"phoneType": "mobile"
},
"email": {
"emailAddress": "TEST#TEST1.com"
}
}]
====
Feature: Read from File
Background:
* def Testdata = read('Data.json')
Scenario Outline: [1] Test with multiple records on JSON
Given url_stg
And path 'test','test'
And request Testdata
When method POST
Examples:
|Testdata |
Please refer it in this documentation as it very straight forward for your case.
dynamic-scenario-outline
refer to this answer for different ways of providing data to Scenario Outline:
https://stackoverflow.com/a/56135752/8615449

Using Groovy in soapUI to extract a JSON field

I am using soapui with groovy for automation api test and I post a request REST, here is my response:
{`enter code here`
"firstname": "aaa",
"lastname": "bbb",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "326"
}, {
"firstname": "ddd",
"lastname": "eee",
"address": "test",
"city": "city",
"country": "country",
"default": "True",
"id": "67"
}, {
"firstname": "hhh",
"lastname": "yyy",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "345"
}, {
"firstname": "ooo",
"lastname": "hh",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "3211"
},
I want to recover the id of the user who has a default "true"
Firstly, the response you posted is not valid json.
Should be something like this:
[
{
"firstname": "aaa",
"lastname": "bbb",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "326"
},
{
"firstname": "ddd",
"lastname": "eee",
"address": "test",
"city": "city",
"country": "country",
"default": "True",
"id": "67"
},
{
"firstname": "hhh",
"lastname": "yyy",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "345"
},
{
"firstname": "ooo",
"lastname": "hh",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "3211"
}
]
If you're just interested in the groovy script, here it is:
package json
import com.eviware.soapui.support.XmlHolder
import groovy.json.*
//define the location of the JSON response
def response = context.expand('${post_json_data#Response}').toString()
//parse response as json
def json_response = new JsonSlurper().parseText(response)
//iterate on the list with if condition
json_response.eachWithIndex {
item, index ->
if(item.default == 'True') {
log.info "index: ${index}"
log.info "Item default value: ${item.default}"
log.info "Item id value : ${item.id}"
}
}