【问题标题】:Populating Table View Cells Creates blank cells填充表格视图单元格创建空白单元格
【发布时间】:2017-08-19 01:06:08
【问题描述】:

我正在用列表中的项目填充表格视图,当我向表格视图添加多个项目时,除了第一个单元格之外的所有单元格都是空白的。当我删除屏幕底部最低的单元格时,它会将来自该单元格的数据放入下一个单元格中。我不确定我做错了什么,但如果有人能够提供帮助,这是我的代码。

import UIKit

// Initialize the list that populates the each cell in the table view
var list = [AnyObject]()
let cell = UITableViewCell(style: UITableViewCellStyle.default, 
reuseIdentifier: "cell")

class FirstViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource {



@IBOutlet weak var myTableView: UITableView!



// Declares how many rows the table view will have
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return (list.count)
}

// Populates each table view cell with items from each index in the list
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

//        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
//        cell.accessoryType = .checkmark
//        cell.textLabel?.textAlignment = .natural

    // The text of the cell = the data of index that matches the index of the cell
    cell.textLabel?.text = list[indexPath.row] as? String

    // Sets the background color of the cell
//        cell.backgroundColor = UIColor.clear

    return cell
}

// Allows user to remove item from table view by swiping left
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
    // if the user swiped left to delete an item from the table view
    if editingStyle == UITableViewCellEditingStyle.delete
    {
        // remove the deleted item from the list and reload the data
        list.remove(at: indexPath.row)
        myTableView.reloadData()
    }
}

【问题讨论】:

  • 您需要从一千个表格视图教程中找到任何一个,并从cellForRowAt 方法中查看提供单元格的正确方法。
  • @maddy 这就是我想要弄清楚的。我看过几个教程。如果您能提供一些关于我的 cellForRowAt 方法做错了什么的见解,那将很有帮助。

标签: ios swift uitableview


【解决方案1】:

步骤 I:- 转到您的故事板。选择 FirstViewController 场景,选择 myTableView。 现在选择属性检查器 设置“原型单元”值 = 1

现在展开“myTableView”并选择“cell”。 再次选择属性检查器 设置标识符值“单元格”。现在你已经完成了故事板。 转到 FirstViewController 并粘贴下面的代码

只需将下面的行粘贴到您的 viewDidLoad() 中

myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

两者都一样

import UIKit

class FirstViewController: UIViewController,UITableViewDelegate,
UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!
var list = ["A", "B", "C","A", "B", "f"]

override func viewDidLoad() {
    super.viewDidLoad()
    myTableView.dataSource = self
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return (list.count)
}

// Populates each table view cell with items from each index in the list
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

    //        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    //        cell.accessoryType = .checkmark
    //        cell.textLabel?.textAlignment = .natural

    // The text of the cell = the data of index that matches the index of the cell
    let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = list[indexPath.row]

    // Sets the background color of the cell
    //        cell.backgroundColor = UIColor.clear

    return cell
}

// Allows user to remove item from table view by swiping left
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
    // if the user swiped left to delete an item from the table view
    if editingStyle == UITableViewCellEditingStyle.delete
    {
        // remove the deleted item from the list and reload the data
        list.remove(at: indexPath.row)
        myTableView.reloadData()
    }
}

}

【讨论】:

  • 你还没有添加数据源,myTableView.dataSource = self.但是在仅使用我们之前的代码添加数据源之后,您将只获得带有列表最后一个值的单行。希望这绝对会帮助你
【解决方案2】:

你错过了很多东西。首先你需要将mytableView的datasourcedelegate设置为self。此外,您需要在viewDidLoad 中初始化list 数组或在运行时调用的任何此类方法。一旦列表数组被初始化。然后您需要致电myTableView.reloadData()。最后你的cellForRow 应该是这样的:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) 
    cell.textLabel?.text = list[indexPath.row] as? String
    return cell
}

【讨论】:

  • 好吧,我在类之外初始化了列表,因为我需要在另一个视图控制器中访问它。 “let cell=...”行也是如此。有没有更好的方法来做到这一点?
  • 您仅将列表声明为 AnyObject,但未使用其中的值进行初始化。因此,在初始化数组后,您需要调用 list.尝试打印列表中的值。
  • 我用 AnyObject 初始化了列表,因为我想要一个只包含用户添加的项目的空列表,特别是字符串。另外,将表视图的数据源和委托设置为 self 有什么作用?抱歉,我对这些东西很陌生。
  • 既然你是新手,我可以推荐一些你可以先学习的教程。
【解决方案3】:

我遇到了同样的问题,如果之前的任何答案都不起作用:我以编程方式进行了大部分设置。

定义全局变量 var cellsContent = [String]() // Change to any type you need

首先在 viewDidLoad 函数中创建带有标识符的单元格:

cellsContent = ["Text1", "Text2"]
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.reloadData()

用 cellsContent var 中的项目填充每个表格视图单元格

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = cellsContent[indexPath.row] as? String
cell.backgroundColor = UIColor.clear

return cell
}

设置每个部分的部分和行

override func numberOfSections(in tableView: UITableView) -> Int {
        return 1 // Change as needed
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellsContent.count
    }

【讨论】: