簡體   English   中英

iOS TableView自定義單元格不顯示任何內容

[英]iOS TableView custom cell not showing anything

我是iOS和Swift的新手,我想通過創建一個簡單的Todo應用程序來學習一些知識。 我遇到的問題是,無論我如何實現代碼(接續多個教程)和情節提要,數據都不會顯示,並且自定義單元格也不會自定義(即使我已自定義,它看起來也像默認單元格一樣)它)。 我已經連接了我的delegatedataSource

編輯:我已經分配了重用標識符

TodosView.swift

import UIKit

class TodosView: UIViewController {
    @IBOutlet weak var todosTable: UITableView!

    var todos: [Todo] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        todosTable.delegate = self
        todosTable.dataSource = self

        self.addTodo()
    }

    func addTodo() {
        let todo1 = Todo(text: "My first todo")
        let todo2 = Todo(text: "My second todo")

        todos = [todo1, todo2]
    }
}

extension TodosView: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return todos.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let todo = todos[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodoCell") as! TodoCell

        cell.setTodo(todo: todo)

        return cell
    }
}

TodoCell.swift

import UIKit

class TodoCell: UITableViewCell {
    @IBOutlet weak var todoText: UILabel!

    func setTodo(todo: Todo) {
        todoText.text = todo.text
    }
}

托多·斯威夫特

import Foundation

struct Todo {
    var text: String
    var done: Bool

    init(text: String, done: Bool = false) {
        self.text = text
        self.done = done
    }
}

我成功地使用您的代碼成功生成了您的Todo行(調用addTodo()之后,我沒有reloadData() addTodo() );

在此處輸入圖片說明

證明您的代碼確實有效后,我使我相信您在Storyboard設置中的某個地方比在代碼本身中更容易遇到問題。 一些建議:

驗證您的自定義單元是否被子類化為TodoCell 您可以通過在Interface Builder中單擊TodoCell來執行此操作,然后在Identity Inspector選項卡中,確認已將此設置為TodoCell:

在此處輸入圖片說明

這可能不是問題,因為如果單元的分類不正確,您的應用很可能會崩潰。

驗證您是否已在Interface Builder中設置了單元標識符 再次,在Interface Builder中單擊TodoCell,轉到Attributes Inspector選項卡,並驗證標識符是否設置為TodoCell:

在此處輸入圖片說明

另外,請確保已將tableView和todoText UILabel確實連接到了代碼。 我看到您在這些項目上有@IBOutlets,但是如果您是從教程中復制和粘貼的,則有可能您鍵入了這些項目而從未真正連接它們。 應該為tableView和UILabel填充IBOutlet旁邊的灰色圓圈,如下所示:

在此處輸入圖片說明

如果為空,則可能沒有連接,這可以解釋該問題。 再次,我逐字復制並粘貼了您的代碼,並根據上述建議進行了設置; 我不相信reloadData()或設置節數可以解決這個問題(因為您的代碼中沒有它們,並且可以正常工作)。

您需要在更新數據源后重新加載表格視圖:-

func addTodo() {
    let todo1 = Todo(text: "My first todo")
    let todo2 = Todo(text: "My second todo")
    todos = [todo1, todo2]
    todosTable.reloadData()
}

編輯通過查看JWC注釋,我還注意到您尚未實現numberOfSection方法,因此還必須添加部分委托方法的數量。

您將在創建tableView之后將數據添加到array

更改此行:

var todos: [Todo] = []

var todos: [Todo]! {
    didSet {
        self.todosTable.reloadData()
    }
}

並在numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard (todos != nil) else {
        return 0
    }
    return todos.count
}

你可以用這個

func addTodo() {
   let todo1 = Todo(text: "My first todo")
   let todo2 = Todo(text: "My second todo")
   todos = [todo1, todo2]
   DispatchQueue.main.async {
      self.todosTable.reloadData()
   }
}

這可能對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM