简体   繁体   中英

add Single Value inside an existing Section in UITableView

i have a tableview with sections for adding section i created an struct :

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

 var datesStructArray = [datesStruct]()

now i want to add a single value inside my exiting sections after checking if the section is satisfying my condition, any idea how am gonna do that ???

eg :

 if myTableViewSection1 == 2016{

 //add value  into this section 

 } 

searched a lot about this but didn't get anything yet , if anybody knows then please do help me thanks

First, put this function outside of your class (before "class" or after the last "}":

func == (leftItem: DatesStruct, rightItem: DatesStruct) -> Bool{
    return leftItem.sectionYear == rightItem.sectionYear
}

Then Use this:

struct DatesStruct: Equatable{
    var sectionYear : String!
    var sectionMonth : [String]!
}

var datesStructArray = [DatesStruct]()

func addMonthToYear(year: String, month: String){
    if let foundItem: DatesStruct = datesStructArray.filter({$0.sectionYear == year}).first {
        datesStructArray[datesStructArray.indexOf(foundItem)!].sectionMonth.append(month)
    }
}

Simply pass the year you're looking for and the month you want to add to addMonthToYear

Declare an initializer

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

        init(sectionYear: String!, sectionMonth: [String]!) {
            self.sectionYear = sectionYear
            self.sectionMonth = sectionMonth
        }
    }

var datesStructArray = [datesStruct(sectionYear: "year", sectionMonth: ["month1", "month2"])]

Then check whether the sectionMonth contains desired string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM