Ios Swift-如何选择进入哪个部分的单元格(UITableView)?
我已作出以下声明:Ios Swift-如何选择进入哪个部分的单元格(UITableView)?,ios,swift,uitableview,Ios,Swift,Uitableview,我已作出以下声明: var data = ["Apple", "Apricot", "Banana", "Blueberry", "Cantaloupe", "Cherry", "Clementine", "Coconut", "Cranberry", "Fig", "Grape", "Grapefruit", "Kiwi fruit", "Lemon", "Lime", "Lychee", "Mandarine", "Mango", "Melon", "Nectarin
var data = ["Apple", "Apricot", "Banana", "Blueberry", "Cantaloupe", "Cherry",
"Clementine", "Coconut", "Cranberry", "Fig", "Grape", "Grapefruit",
"Kiwi fruit", "Lemon", "Lime", "Lychee", "Mandarine", "Mango",
"Melon", "Nectarine", "Olive", "Orange", "Papaya", "Peach",
"Pear", "Pineapple", "Raspberry", "Strawberry"]
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"]
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 12
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellLabel", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
cell.textLabel?.text = data[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return months[section]
}
现在我如何选择数据数组中的哪些变量进入哪个“月”部分?是否有一种我不知道的方法我应该使用?
(这是在扩展UITableView的类中)func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int
和tableView(tableView:UITableView,cellforrowatinexpath indexath:nsindexath)->UITableViewCell
有section和indexath参数,indexath有section
变量。因此,基本上,您应该在这些函数中执行一些switch语句,以根据月份返回所需的数据。最好是每个月都有一组数据。或多维数组,甚至是某些自定义数据类型。
无论如何,代码可能类似于:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return dataJanuary.count
}
else if section == 1 {
return dataFebruary.count
}
// ...
return 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellLabel", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
if indexPath.section == 0 {
cell.textLabel?.text = dataJanuary[indexPath.row]
}
else if indexPath.section == 1 {
cell.textLabel?.text = dataFebruary[indexPath.row]
}
// ...
return cell
}
你到底想实现什么?太棒了!非常感谢您的澄清。我知道我想创建某种键值数据类型,但不知道是什么方法控制了位置。非常感谢!