想要添加新单元格。我怎样才能做到这一点? 请帮忙! 我试试
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 1 {
return 3 + eventDates.count
} else {
return super.tableView(tableView, numberOfRowsInSection: section)
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.section == 0 && indexPath.row == 1) {
eventDaysVisible = !eventDaysVisible
tableView.reloadData()
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: eventDates.count + 3, inSection: 0)], withRowAnimation: .Automatic)
tableView.reloadData()
}
for day in eventDates {
print("OBJ: \(day)")
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
我有3个静态单元,如果我切换按钮,则需要在第一个之后插入新单元格。
var eventdates = Friday, Saturday, Sunday
希望你能帮助我。
答案 0 :(得分:1)
import UIKit
class ViewController: UIViewController {
var cellCount = 1
@IBOutlet weak var tableView: UITableView!
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.
}
}
extension ViewController:UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellCount
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.grayColor()
return cell
}
func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let eventDates = ["1", "2", "3"]
var indexPathsArray = [NSIndexPath]()
for rowCount in 0 ..< eventDates.count {
indexPathsArray.append(NSIndexPath(forRow: rowCount, inSection: 0))
cellCount += 1
}
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths(indexPathsArray, withRowAnimation: .Automatic)
tableView.endUpdates()
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}