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 :
我有一个数组,在这个[“April:2016:P 2:A 28”]模式中有一些值,我想在这里添加2016年的年份,作为一个部分,这样我可以有一个tableView像:
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
在输出中没有任何部分,即使现在我在我的桌子上有重复的记录?任何想法如何使用记录中的值添加部分看起来像这样[“April:2016:P 2:A 28”]任何帮助将非常感激
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 个解决方案
#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...
}
#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...
}