首页 > 解决方案 > 从 TableView 中调用了两次 TableViewCell 数据

问题描述

我想从 TableView 获取数据到 Cell。所以我cell.count = groups[indexPath.row].count在dataSource中写了。在 TableViewCellvar count: Int!中声明。当我在 layoutSubviews() 中打印计数时,它会两次调用相同的数据,所以我不能做我想做的事。当我在 dataSource 方法中打印 groups[indexPath.row].count 时,它会正常打印一次。我怎么解决这个问题?

示例)如果计数序列为 1 2 1,则在单元格计数变量中打印 1 1 2 2 1 1

标签: iosswift

解决方案


我想我写了代码,因为你的写作中没有代码。

let groups : [Int] = [1,2,3,4,5]

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.groups.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let rowData = self.groups[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "GROUP_CELL")
        cell?.textLabel?.text = rowData
        return cell!
    }

这不会两次输出“组”的内容。这是你想要的吗?


推荐阅读