Swift 3 使用 uialertcontroller 向 uitableviewcontroller 添加新行

萨姆·卡诺

我正在构建一个待办事项列表应用程序。我正在尝试单击添加按钮添加待办事项。它打开一个带有输入标题的警报。我为添加的 Item 创建了一个类:

class ToDoItem
{
    var title: String


    public init(title: String)
    {
        self.title = title
    }
}

这是我添加新行的代码:

func didTapAddItemButton(_ sender: UIBarButtonItem)
    {

        let alert = UIAlertController(
            title: "New to-do item",
            message: "Insert the title of the new to-do item:",
            preferredStyle: .alert)


        alert.addTextField(configurationHandler: nil)

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))


        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
            if let title = alert.textFields?[0].text
            {
                self.addNewToDoItem(title: title)
            }
        }))


        self.present(alert, animated: true, completion: nil)
    }

    private func addNewToDoItem(title: String)
    {

        let newIndex = listCourse?.count


        listCourse?.append(ToDoItem(title: title))


        tableView.insertRows(at: [IndexPath(row: newIndex!, section: 0)], with: .top)
    }

但我得到了那个错误:

Cannot convert value of type "ToDoItem" to expected argument type "myItems"

这是 myItems 类:

class myItems {
    var title: String?
    var content: String?
    var date: String?
    var author: String?


    init(title: String, content: String, date: String, author: String){
        self.title = title
        self.content = content
        self.date = date
        self.author = author
    }

    func mapping(map: Map) {
        title <- map["title"]
        content <- map["description"]
        date <- map["pubDate"]
        author <- map["author"]
    }
}

这个“myItems”类是用来获取json数据的,后面我会从数据库中获取数据。

最后一件事 :

var listCourse : [myItems]?

listCourse 是在 tableViewController 中显示的单元格列表。

我不明白那个错误我只想添加在列表中输入的标题

[编辑]

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "macell1", for: indexPath)
        return cell
    }
忍者程序

您正在尝试将类型错误的项目附加到 listCourse。由于您将 listCourse 定义为 myItems 实例的数组,因此您只能向它附加该类的实例。要修复它只需更改var listCourse : [myItems]?var listCourse = [ToDoItem]()

还要修复您的 UITableViewDataSource 方法:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "macell1", for: indexPath)
    let item = listCourse[indexPath.item]
    cell.textLabel.text = item.title
    return cell
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在Swift中的UITableViewController中呈现UIAlertController

swift 3 使用 ViewController 中的 textField 向 TableViewController 添加行

使用 UILongPressGestureRecognizer swift3 向 GoogleMaps 添加标记

使用JavaScript向表添加新行

如何向UITableViewController添加粘性页脚?

向已经显示的UIAlertController添加动作

使用Swift向NSImage添加模糊

使用Swift向CGContext添加文本

Swift 3中的UIAlertController中的自动换行

使用Javascript向表中添加新行

使用特定的索引名称向Pandas DataFrame添加新行

使用jQuery向表中添加新行

如何使用PPI向Perl脚本添加新行?

使用for循环向表中添加新行:python

如何在Swift 3中向CNMutableContact添加新电子邮件?

使用 Swift 4 以编程方式将 UIView 添加到带有 NSLayoutConstraints 的 UITableViewController

Swift 3 - 向 UINavigationController 子类添加导航项

如何向 UIAlertController 添加倒数计时器

如何使用 SWIFT 向包含 UILabel 的 UITextField 正确添加约束?

使用 Swift(4.0) 向 .xib 文件中的按钮添加操作

使用swift 2.2以编程方式向UITableView添加标头

如何使用 Swift 向 XCode 项目添加动画加载器?

向文件添加新行

使用kld向freebsd添加新的syscall

如果表已经多于一行,如何使用jquery向表中添加新行

UIAlertController在Swift 3的启动视图中不起作用

每页向IText文档添加3行

Python 使用基于前 3 行的滚动窗口向 DataFame 添加列

D3.js-如何在此可折叠树中向文本添加新行?