传递数据给自定义的UITableViewCell类

3

ViewControllerA将会推出ViewControllerB。ViewControllerB是一个UITableView,其中包含一个在加载时注册的自定义UITableViewCell

这是我的自定义UITableViewCell代码:

override func awakeFromNib() {
        super.awakeFromNib()
    // Here is me attempting to pass my data from my view controller
    someButton.text = stringFromViewController
}

这是我的UITableView

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let customCell = CustomCell()
    customCell.stringFromViewController = "Some Text"
    self.performSegueWithIdentifier("ViewControllerB", sender: nil)    
}

每当我尝试这样做时,string总是返回为空。我不确定这是否与我正在尝试将此数据传递给包含自定义cellUITableView而不是UITableViewCell有关。我想将数据传递给我的自定义cell类。

我在你的问题中没有看到任何关于表格数据源的参考。你能否添加更多关于tableView的代码? - Alessandro Ornano
5个回答

1
你的做法是错误的。 在加载控制器B时,你的tableView会自动重新加载。 你应该在tableView:CellForRow:方法中将数据传递给单元格。 在从控制器A分配值后,你也可以调用tableView.reloadData()。 这是一个例子:
class ControllerB {
  var tableView: UITableView! //IBOutlet?
  var stringFromA: String? {
      didSet {
        self.tableView.reloadData()
      }
 }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")
    cell.stringFromA = self.stringFromA
    return cell
}

我应该补充一下,uitableviewcell类中有一些自定义方法来处理一些约束。我可以从cellForRowAtIndexPath访问它们吗? - butter_baby
你应该在单元格的自定义类中处理单元格的布局和设计更新,而不是在viewController中处理,这样更符合MVC架构的逻辑。在单元格类(UIView)中有几个方法可以重写:awakeFromNib()、layoutSubview()、didMoveToSuperview(),每个方法都有其自身的优点。 - Lirik

0

我同意 @Lirik 的观点

在你的ControllerB的属性上添加属性监听器并重新加载表格视图,然后在cellForRowAtIndexPath(::)中将出列的单元格转换为您的自定义单元格类型,以便可以访问您单元格的所有功能

let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! CustomCell

cell.someBotton.text = stringFromViewControllerA


0

我同意@Lirik的观点

在您的控制器B上设置一个属性观察器并重新加载表格视图,然后在您的cellForRowAtIndexPath(::)

将出列的单元格强制转换为您自定义的单元格类型,以便您可以访问您的单元格的所有函数

let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! CustomCell


0

因为awakeFromNib()在你初始化cell之前就已经被初始化了。

所以,不要在这个方法中分配它,而是在didSelectRowAtIndexPath中使用它。

let customCell = CustomCell()

customCell.someButton.setText("Some Text", forState: .Normal)

self.performSegueWithIdentifier("ViewControllerB", sender: nil) 

1
好的,假设我想传递一个布尔值,在awakeFromNib()中会用到它。应该如何实现? - butter_baby

0

我理解你的问题是,从视图控制器A点击一行将推出视图控制器B,并且你想要传递一些数据到视图控制器B中的自定义单元格。你需要使用prepareForSegue来传递数据。

在视图控制器B中,设置一个变量,而在你的情况下,是一个布尔值。

class ViewControllerB {

var boolValue: Bool?

}

然后在ViewControllerA中的didSelectRowAtIndexPath方法中执行segue操作。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

self.performSegueWithIdentifier("ViewControllerB", sender: nil)    
}

接下来您需要根据所选择的行执行prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    if segue.identifier == "ViewControllerB" {
        //if you need to know which row is select, use indexPath.row
        if let indexPath = tableView.indexPathForSelectedRow {
            let controller = segue.destinationViewController as! ViewControllerB
            //send the details that you want, i.e
            if indexPath.row < 5 {
                controller.boolValue = true
            } else {
                controller.boolValue = false
            }
        }
    }

一旦调用了segue,您可以通过prepareForSegue传递数据,然后使用cellForRowAtIndexPath方法将信息传递给单元格。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomTableViewCell
        //pass the boolValue from viewcontrollerB to anotherBoolValue in custom cell
        cell.anotherBoolValue = boolValue

    // Configure the cell...

    return cell
}

我要访问的属性不在ViewController B中,而是在我的自定义表视图单元格中。 - butter_baby
可以通过cellForRowAtIndexPath方法来将变量从B视图控制器传递给自定义单元格。或者你可以更清楚地表述问题,说明变量的名称、你命名的变量以及它们的位置,这样就更容易帮助你了。 - Chris
我已经在cellForRowAtIndexPath方法中添加了代码。这是假设您已经为自定义单元格创建了一个类,并在其中添加了变量anotherBoolValue - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接