【发布时间】:2017-10-09 06:12:26
【问题描述】:
我想创建带有折叠和 exapnnd 的表格视图单元格。但我有一些错误。我需要一些帮助!下面是我的代码希望你不介意我是 swift2 的新手
我正在使用 swift2。
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableview: UITableView!
想以sectionArr为标题
let sectionArr = ["animal", "bird"] //section
let animalArr = ["dog", "cat"]
let birdArr = ["crow", "cock"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionArr.count
}
//标题部分
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 80)
// headerView.backgroundColor = [UIColor, clearColor];
let lblTitle = UILabel()
lblTitle.frame = CGRectMake(0, 0, tableView.frame.size.width, 40)
lblTitle.text = sectionArr[section]
headerView.addSubview(lblTitle)
return headerView
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return animalArr.count
} else {
return birdArr.count
}
}
//显示视图单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell1:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
//cell1.textLabel!.text=sectionArr[indexPath.row]
cell1.textLabel!.text=animalArr[indexPath.row]
cell1.textLabel!.text=birdArr[indexPath.row]
return cell1
}
保存选定的索引路径行
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let Toy = sectionArr[indexPath.row]
print(Toy)
}
}
【问题讨论】:
-
把错误和它们何时出现。如果您是 swift 新手,那么您应该选择 swift 4,最好是最新的
标签: swift uitableview swift2