我正在构建一个跟踪用户事件的日历应用程序。一旦用户按下橙色的大按钮,就会在仪表板页面上添加新的表格视图单元格,并且用户将能够编辑事件。但是当我尝试使用加号按钮时,什么也没有发生。有人能帮我吗?? 谢谢!
这是我的代码
仪表板ViewController --------------------------------------------- -
//
// DashboardViewController.swift
// Pursuit 1.0
//
// Created by Ryan Du on 9/9/18.
// Copyright © 2018 Ryan Du. All rights reserved.
//
import UIKit
class DashboardViewController: UIViewController, cellInfoDelegate, UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return events.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Dcells = events[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardCell") as! DashboardCell
cell.setDashboardCell(cell: Dcells)
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableCellInfo(title: String?, description: String?) {
}
@IBOutlet weak var DashboardAdd: UIButton!
@IBOutlet weak var tableView: UITableView!
@IBAction func DashboardAddA(_ sender: Any) {
events.append(Dashboard(title: "Untitled", description: "Add A Description Here"))
tableView.register(DashboardCell.self, forCellReuseIdentifier: "DashboardCell")
}
var events: [Dashboard] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func createArray(titleOfEvent: String, descriptionOfEvent: String) -> [Dashboard] {
var Events: [Dashboard] = []
let event = Dashboard(title: titleOfEvent, description: descriptionOfEvent)
Events.append(event)
return Events
}
}
仪表板单元---------------------------------------------- ------------
//
// DashboardCell.swift
// Pursuit 1.0
//
// Created by Ryan Du on 9/20/18.
// Copyright © 2018 Ryan Du. All rights reserved.
//
import UIKit
protocol cellInfoDelegate {
func tableCellInfo(title: String?, description: String?)
}
class DashboardCell: UITableViewCell{
let cellInformationDelegate: cellInfoDelegate!
@IBOutlet weak var cellTitle: UITextField!
@IBOutlet weak var cellDescription: UITextField!
func setDashboardCell (cell: Dashboard) {
cellTitle.text = cell.title
cellDescription.text = cell.desccription
}
@IBAction func cellTitleEdited(_ sender: Any) {
if cellDescription.text != "" && cellTitle.text != "" {
cellInformationDelegate.tableCellInfo(title: (cellTitle.text)!, description: (cellDescription.text)!)
}
if cellTitle.text != "" {
cellInformationDelegate.tableCellInfo(title: (cellTitle.text)!, description: nil)
}
}
@IBAction func cellDescriptionEdited(_ sender: Any) {
if cellDescription.text != "" && cellTitle.text != "" {
cellInformationDelegate.tableCellInfo(title: (cellTitle.text)!, description: (cellDescription.text)!)
}
if cellDescription.text != "" {
cellInformationDelegate.tableCellInfo(title: nil, description: (cellDescription.text)!)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
仪表板---------------------------------------------- ---------
//
// Dashboard.swift
// Pursuit 1.0
//
// Created by Ryan Du on 9/27/18.
// Copyright © 2018 Ryan Du. All rights reserved.
//
import Foundation
import UIKit
class Dashboard {
var title: String
var desccription: String
init(title: String, description: String){
self.title = title
self.desccription = description
}
}
答案 0 :(得分:2)
不是注册单元格而是重新加载表格视图。
@IBAction func dashboardAddA(_ sender: Any) {
events.append(Dashboard(title: "Untitled", description: "Add A Description Here"))
tableView.reloadData()
}
或在其中插入动画
@IBAction func dashboardAddA(_ sender: Any) {
let indexPath = IndexPath(row: events.count, section: 0)
events.append(Dashboard(title: "Untitled", description: "Add A Description Here"))
tableView.insertRows(at: [indexPath], with: .automatic)
}
请根据命名约定以小写字母开头的方法命名