ios - 我设置了它,但它不确认 UITableViewDataSource

标签 ios swift uitableview

我设置了 tableview 数据源,但它无法识别他的错误 类型“ViewController”不符合协议(protocol)“UITableViewDataSource”

import UIKit

class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{



    override func viewDidLoad() {
        super.viewDidLoad()

        var contact : [pedped]=[]

        let asghar = pedped()
        asghar.nmae="asghar "
        asghar.lname="ghasemi"
        asghar.image="asghar"

        let akbar = pedped()
        akbar.nmae="akbar "
        akbar.lname="askj"
        akbar.image="asgkajsjkashar"


        contact.append(asghar)
        contact.append(akbar)



        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return contact.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell=tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath)as!custom
            let contacts = contact[indexPath.row]
            cell.lablename.text = contacts.nmae
            cell.lastnamelable.text = contacts.lname
            return cell
        }

    }


}

最佳答案

尽管 ViewController 符合 UITableViewDataSource,但您在错误 的地方实现了这些方法。你必须在 viewDidLoad 之外移除它们。

此外,确保 TableView 数据源/委托(delegate)连接到 View Controller 本身,无论是从界面构建还是从代码:

// create an IBOutlet for the table view then:
tableView.dataSource = self
tableView.delegate = self

关于ios - 我设置了它,但它不确认 UITableViewDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55837332/

相关文章:

ios - 当再次显示 viewcontroller 时在 uitableviewcell 上渲染 View

ios - MKMapView 中的所有 Annotation 对用户不可见

ios - if 语句中的错误 bool 结果

ios - 在 Swift 中优化从 Firestore 获取

ios - 关闭删除多个 View

ios - 使用 UITableView 中的核心图形将图像过滤器应用于图像

c++ - 架构 arm64 的 undefined symbol 。对 ObjC++ 文件进行 C 调用?

iOS 11 无法禁用复制粘贴选项 UITextField

ios - UICollectionView didSelectIotemAt issue

ios - 如何理解 table.autoresizingMask 的行为?