1

i have an array which haves some values in this ["April : 2016 : P 2 : A 28"] pattern , i want to add the year which is 2016 here , as an section so that i can have a tableView like :

2016
April : 2016   : P 92  : A 528
March : 2016   : P 42  : A 128
May : 2016   : P 12  : A 238
June : 2016   : P 23  : A 268

2017
Jan : 2016   : P 92  : A 528
April : 2016   : P 42  : A 128
Dec : 2016   : P 12  : A 238
Oct : 2016   : P 23  : A 268

how am gonna do that i tired this

i created a struct

    struct  datesStruct {
    var sectionYear : String!
    var sectionMonth : [String]!
}

        for set in setOfMonths {

        datesStructArray.append(datesStruct(sectionYear: "\(yearInt)", sectionMonth: newArrayofValues))
    // here `yearInt` is the year which have to be my section for records and i pulled it from the `newArrayofValues` array
        tableView.reloadData()
    }

in output there's no section even now i have duplicated record on my table ? any idea how can i add section using a value from a record which looks like this ["April : 2016 : P 2 : A 28"] any help would be much appreciated

update:

now am getting section but sections are repeating (duplicate entry of same section) is my code:

    for set in setOfMonths {

     datesStructArray.append(datesStruct(sectionYear: "\(yearInt)", sectionMonth: newArrayofValues)) //  here newArrayOfValue is an array
    tableView.reloadData()
      }

my output:

2016
Jan : 2016   : P 92  : A 528
2016
April : 2016   : P 42  : A 128
2016
Dec : 2016   : P 12  : A 238
2017
Oct : 2017   : P 23  : A 268

but what i want is :

2016
Jan : 2016   : P 92  : A 528
April : 2016   : P 42  : A 128
Dec : 2016   : P 12  : A 238

2017
Oct : 2017   : P 23  : A 268

1 Answer 1

1

OK, you will need to maintain the data as a dictionary. Key being the year, and the values being an array of string objects.

For example-

var dataDict = ["2017" : ["April : 2016   : P 92  : A 528", 
                          "March : 2016   : P 42  : A 128"],

                "2016" : ["Jan : 2016   : P 92  : A 528", 
                          "Feb : 2016   : P 42  : A 128"]]

Now you can return number of sections in the data source method as:

func numberOfSectionsInTableView(_ tableView: UITableView) -> Int{
    return dataDict.keys.count
}

You will need to maintain a sorted list of the section names, and supply them as section titles:

let sortedSectionNames = dataDict.keys.sort()

You pass the section titles in the following data source method-

 func sectionIndexTitlesForTableView(_ tableView: UITableView) -> [String]?{
    return sortedSectionNames
}

Now you need to return the configured cells for rows for sections:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //Dequeue the cell...

    //Get the appropriate model-
    let section = indexPath.section
    let dataArray = dataDict[sortedSectionNames[section]]!
    let stringForRow = dataArray[indexPath.row]

    //Set this string into a label in your cell...
}
Sign up to request clarification or add additional context in comments.

4 Comments

hey thanks a lot ! for your effort i really appreciate it and i think this is what i need but can you take a look on my updated question ? because i think i came too long and am about to get my solution
hey @shripada i tried according to your answer but the output is showing only a single section and column ?? any idea whats wrong should i show you my code ??
dataDict.keys.count is returning 1 , know why ?
thanks bro i found another way thanks for helping , appreciate you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.