I have two arrays Data1 and Data2 and I want to populate the data within each of these (they contain strings) into a tableview in two different sections. The first section should have a heading "Some Data 1" and the second section should be titled "KickAss".
我有两个数组Data1和Data2,我想将每个数组(它们包含字符串)中的数据填充到两个不同部分的tableview中。第一部分应该有标题“Some Data 1”,第二部分应该有标题“KickAss”。
I have both sections populating with data from the first array (but with no headings either).
我有两个部分都使用了第一个数组中的数据(但也没有标题)。
Here is my code so far:
这是我目前的代码:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowCount = 0
if section == 0 {
rowCount = Data1.count
}
if section == 1 {
rowCount = Data2.count
}
return rowCount
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let ip = indexPath
cell.textLabel?.text = Data1[ip.row] as String
return cell
}
in the cellForRowAtIndexPath method, is it possible for me to identify the section somehow like I did in the numberOfRowsInSection method? Also, how do I give titles to each section? Thanks
在cellForRowAtIndexPath方法中,我是否可以像在numberOfRowsInSection方法中那样以某种方式识别部分?另外,我如何给每个部分赋予标题?谢谢
3 个解决方案
#1
12
You can determine which section you are in by looking at indexPath.section
. To specify the titles, override the function
您可以通过查看indexPath.section来确定您所在的部分。要指定标题,请重写函数
func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String!
#2
57
TableView Cells
TableView细胞
You could use a multidimensional array. For example:
可以使用多维数组。例如:
let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"]]
For the number of sections use:
对于使用的节数:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return data.count
}
Then, to specify the number of rows in each section use:
然后,要指定每个部分的行数,请使用:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
Finally, you need to setup your cells:
最后,您需要设置单元格:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellText = data[indexPath.section][indexPath.row]
// Now do whatever you were going to do with the title.
}
TableView Headers
TableView头
You could again use an array, but with just one dimension this time:
你可以再次使用数组,但这一次只有一个维度:
let headerTitles = ["Some Data 1", "KickAss"]
Now to set the titles for the sections:
现在为章节设置标题:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < headerTitles.count {
return headerTitles[section]
}
return nil
}
The code above checks to see there's a title for that section and returns it, otherwise nil
is returned. There won't be a title if the number of titles in headerTitles
is smaller than the number of arrays in data
.
上面的代码检查是否有该部分的标题并返回它,否则返回nil。如果标题中的标题数量小于数据中的数组数量,就不会有标题。
The Result
结果
#3
20
You could create a Struct
to hold the data that belongs to a section, as an alternative to my previous answer. For example:
您可以创建一个Struct来保存属于某个节的数据,作为对我之前答案的替代。例如:
struct SectionData {
let title: String
let data : [String]
var numberOfItems: Int {
return data.count
}
subscript(index: Int) -> String {
return data[index]
}
}
extension SectionData {
// Putting a new init method here means we can
// keep the original, memberwise initaliser.
init(title: String, data: String...) {
self.title = title
self.data = data
}
}
Now in your view controller you could setup your section data like so:
现在在你的视图控制器中你可以像这样设置你的部分数据:
lazy var mySections: [SectionData] = {
let section1 = SectionData(title: "Some Data 1", data: "0, 1", "0, 2", "0, 3")
let section2 = SectionData(title: "KickAss", data: "1, 0", "1, 1", "1, 2")
return [section1, section2]
}()
Section Headers
节标题
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return mySections.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return mySections[section].title
}
Compared to my previous answer, you now don't have to worry about matching the number of headerTitles
to the number of arrays in data
.
与我之前的答案相比,现在您不必担心头标题的数量与数据中的数组数量的匹配。
TableView Cells
TableView细胞
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mySections[section].numberOfItems
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellTitle = mySections[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = cellTitle
return cell
}
#1
12
You can determine which section you are in by looking at indexPath.section
. To specify the titles, override the function
您可以通过查看indexPath.section来确定您所在的部分。要指定标题,请重写函数
func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String!
#2
57
TableView Cells
TableView细胞
You could use a multidimensional array. For example:
可以使用多维数组。例如:
let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"]]
For the number of sections use:
对于使用的节数:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return data.count
}
Then, to specify the number of rows in each section use:
然后,要指定每个部分的行数,请使用:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
Finally, you need to setup your cells:
最后,您需要设置单元格:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellText = data[indexPath.section][indexPath.row]
// Now do whatever you were going to do with the title.
}
TableView Headers
TableView头
You could again use an array, but with just one dimension this time:
你可以再次使用数组,但这一次只有一个维度:
let headerTitles = ["Some Data 1", "KickAss"]
Now to set the titles for the sections:
现在为章节设置标题:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < headerTitles.count {
return headerTitles[section]
}
return nil
}
The code above checks to see there's a title for that section and returns it, otherwise nil
is returned. There won't be a title if the number of titles in headerTitles
is smaller than the number of arrays in data
.
上面的代码检查是否有该部分的标题并返回它,否则返回nil。如果标题中的标题数量小于数据中的数组数量,就不会有标题。
The Result
结果
#3
20
You could create a Struct
to hold the data that belongs to a section, as an alternative to my previous answer. For example:
您可以创建一个Struct来保存属于某个节的数据,作为对我之前答案的替代。例如:
struct SectionData {
let title: String
let data : [String]
var numberOfItems: Int {
return data.count
}
subscript(index: Int) -> String {
return data[index]
}
}
extension SectionData {
// Putting a new init method here means we can
// keep the original, memberwise initaliser.
init(title: String, data: String...) {
self.title = title
self.data = data
}
}
Now in your view controller you could setup your section data like so:
现在在你的视图控制器中你可以像这样设置你的部分数据:
lazy var mySections: [SectionData] = {
let section1 = SectionData(title: "Some Data 1", data: "0, 1", "0, 2", "0, 3")
let section2 = SectionData(title: "KickAss", data: "1, 0", "1, 1", "1, 2")
return [section1, section2]
}()
Section Headers
节标题
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return mySections.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return mySections[section].title
}
Compared to my previous answer, you now don't have to worry about matching the number of headerTitles
to the number of arrays in data
.
与我之前的答案相比,现在您不必担心头标题的数量与数据中的数组数量的匹配。
TableView Cells
TableView细胞
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mySections[section].numberOfItems
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellTitle = mySections[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = cellTitle
return cell
}