Woo*_*ody 29 arrays uitableview sections swift
我有两个数组Data1和Data2,我希望将这些数据中的数据(它们包含字符串)填充到两个不同部分的tableview中.第一部分应标题为"Some Data 1",第二部分标题为"KickAss".
我有两个部分填充第一个数组的数据(但没有标题).
到目前为止,这是我的代码:
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
}
Run Code Online (Sandbox Code Playgroud)
在cellForRowAtIndexPath方法中,我是否可以像在numberOfRowsInSection方法中那样识别该部分?另外,我如何为每个部分提供标题?谢谢
ABa*_*ith 67
TableView单元格
您可以使用多维数组.例如:
let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"]]
Run Code Online (Sandbox Code Playgroud)
对于部分的数量使用:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return data.count
}
Run Code Online (Sandbox Code Playgroud)
然后,要指定每个部分中使用的行数:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
Run Code Online (Sandbox Code Playgroud)
最后,您需要设置您的单元格:
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.
}
Run Code Online (Sandbox Code Playgroud)
TableView标题
你可以再次使用一个数组,但这次只有一个维度:
let headerTitles = ["Some Data 1", "KickAss"]
Run Code Online (Sandbox Code Playgroud)
现在设置部分的标题:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < headerTitles.count {
return headerTitles[section]
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
上面的代码检查是否有该部分的标题并将其返回,否则nil返回.如果标题的数量headerTitles小于数组中的数量,则不会有标题data.
结果

ABa*_*ith 23
您可以创建一个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
}
}
Run Code Online (Sandbox Code Playgroud)
现在在视图控制器中,您可以设置您的节数据,如下所示:
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]
}()
Run Code Online (Sandbox Code Playgroud)
章节标题
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return mySections.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return mySections[section].title
}
Run Code Online (Sandbox Code Playgroud)
与我之前的回答相比,您现在不必担心匹配headerTitles数量与数组的数量data.
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
}
Run Code Online (Sandbox Code Playgroud)
Glo*_*del 14
您可以通过查看来确定您所在的部分indexPath.section.要指定标题,请覆盖该功能
func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57743 次 |
| 最近记录: |