ios - UILabel 未使用 UITableViewCell 自定义类 Swift 进行更新

标签 ios swift uitableview uilabel

我是 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 - 我在 Storyboard 中附加的标签。

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

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

最佳答案

我认为你应该这样做:

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

关于ios - UILabel 未使用 UITableViewCell 自定义类 Swift 进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37841132/

相关文章:

iphone - 如何设计圆角框和分隔符(如应用程序设置 View )?

iOS swift : Static Cell

ios - 在 iPhone 应用程序中单击平面图区域?

ios - 具有不同覆盖层的单个 viewController 上的多个摄像头

ios - 检查 UITableViewAutomaticDimension 返回 -1

swift - 是否可以通过 openCv 在 iOS Swift 应用程序中使用 Realm 数据库?

iphone - UITableViewCell。如何以编程方式将 textLabel 对齐到顶部?

iphone - 创建条件转场

c# - 从 UIViewController 派生的类中的 Monotouch Dialog 实现

ios - 在 Xcode 7 中,Swift 无法自动完成 Objective-C 代码