gpt4 book ai didi

ios - TableView 不附加 TableView 单元格

转载 作者:行者123 更新时间:2023-11-28 11:45:21 24 4
gpt4 key购买 nike

我正在构建这个跟踪用户事件的日历应用程序。在仪表板页面上,一旦用户按下橙色大按钮,就会附加一个新的表格 View 单元格,用户将能够编辑事件。但是当我尝试加号按钮时什么也没有发生。有人能帮我吗??谢谢!!

这是我的代码👇

仪表板 View Controller -------------------------------------------- -

//
// 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
}
}

最佳答案

而不是注册单元格重新加载表格 View 。

@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)
}

请按照命名约定以小写字母开头的方法命名

关于ios - TableView 不附加 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52691342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com