【问题标题】:UILabel is not updated with UITableViewCell custom class SwiftUILabel 未使用 UITableViewCell 自定义类 Swift 进行更新
【发布时间】:2016-06-15 16:39:26
【问题描述】:

我是 Swift 编程的新手,所以不要评价我很强。我花了很多时间试图了解问题出在哪里,但我无法理解。

我在 UIViewController 中有一个 UITableView

UIViewController:

@IBOutlet weak var myTableView: UITableView!

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



func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 10
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell

    cell.createNewCell()
    return cell
}

在我的自定义 UITableViewCell 类中,我有 myLabel - 我在情节提要中附加的标签。

@IBOutlet weak var myLabel: UILabel!
  func createNewCell(){
        myLabel.text = "1234567"
    }

但是,如果我在单元格更新时将以下行 cell.textLabel!.text = "12312" 添加到 cellForRowAtIndexPath,则当我尝试通过自定义 UITableViewCell 类更新单元格时,单元格不会更新。

【问题讨论】:

  • 我编辑了你的问题,仍然不清楚你在问什么。你最后2句话不清楚

标签: ios swift uitableview uilabel


【解决方案1】:

我认为你应该这样做:

1) 创建您的自定义单元格

class customCell: UITableViewCell
{
    @IBOutlet weak var myLabel: UILabel!
}

2) 为可重用单元格和在 cellForRowAtIndexPath 中设置标识符

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! customCell
    cell.myLabel.text = "1234"
    return cell

【讨论】:

  • 不,你可以看到 MyTableViewCell 已经这样创建了
【解决方案2】:

如果要更新单元格中的数据,则必须重新加载 tablewView。 如果您的数据源数组已更新,并且您想在 tableview 中填充更新的数据,那么只需重新加载tableview。

    @IBOutlet weak var myTableView: UITableView!
    var dataSource= [String]()
    override func viewDidLoad() {
      super.viewDidLoad()
     myTableView.delegate = self
     myTableView.dataSource = self
     dataSource = ["item1", "item2", "item3"]
   }

   func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
   }

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


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell

      cell.myLabel.text = dataSource[indexPath.row]
      return cell
  }
     //Suppose in some function you changed the datasource
    func updateData() {
      dataSource = ["number1", "number2", "number3"]
     //Now just call 
     myTableView.reloadData() 

}

【讨论】:

  • 不,问题是没有显示初始数据,我不需要reloadData()来显示初始数据
【解决方案3】:

我找到了问题所在。实际上,我的 UITableView 位于 tabBarController 内部的 UIViewController 内部。这就是 Xcode 7.3.1 中没有在运行时应用约束的原因。

我通过将所有约束和单元格中的 UILabel 设置为 true 来修复它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    相关资源
    最近更新 更多