从一个不同类swift中的一个类更改属性(特别是UILabel)

时间:2015-08-09 03:36:35

标签: ios uitableview class uilabel swift2

我正在尝试从另一个类内部更改UITableView(一个类的属性)中的UILabel文本,但是我遇到了麻烦。代码看起来像这样(问题在最后,在myDataSource的didSelectRowAtIndexPath方法中)

视图控制器

class ViewController: UIViewController
{
    //MARK: Properties
    @IBOutlet weak var myTableView: UITableView!
    @IBOutlet weak var myLabel: UILabel!

    //needed so data from myDataSource is retained by ViewController and not just thrown away
    let importedDataSource = myDataSource()


    override func viewDidLoad()
    {
        super.viewDidLoad()

        myTableView.dataSource = myDataSource()
        myTableView.delegate = myDataSource()

    }

   override func didReceiveMemoryWarning()
       {super.didReceiveMemoryWarning()}

}

UITableViewDataSource和Delegate

class myDataSource: NSObject, UITableViewDataSource, UITableViewDelegate
{
    let cellIdentifier = "myTableViewCell"
    let myArray = ["Label one", "Label two", "Label three"]

    //MARK: TableViewDataSource
    func numberOfSectionsInTableView(tableView: UITableView) -> Int
     {return 1}

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

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

        cell.myCellLabel.text = myArray[indexPath.row]

        return cell
    }
    //MARK:TableView Delegate
   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
     {  
          //This Line Here returns the error
          ViewController().myLabel.text = "test"
          //I want the end product to be ViewController().myLabel.text = myArray[indexPath.row], but left it as "test" just to simplify and isolate the error
     }

}

(还有一个名为myTableViewCell的UITableViewCell类,但我把它留得更短)

在选择其中一行时,运行返回“致命错误:在展开可选值时意外发现nil”。我该如何调用myLabel以避免此问题?我已经尝试通过ctrl-从故事板中拖动来将它作为@IBOutlet挂在myDataSource中,但正如我所料,它只允许我将它连接到视图控制器。

抱歉,这两个同名的标签代码有点令人困惑。我试图调用在ViewController中创建的myLabel var(第一段代码),而不是在myTableViewCell中创建的myCellLabel(未显示)

2 个答案:

答案 0 :(得分:0)

您需要获取被轻击的实际表格视图单元格才能访问它的变量。使用cellForRowAtIndexPath。它看起来像这样:

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if let myCell = tableView.cellForRowAtIndexPath(indexPath) as? myTableViewCell {
        myCell.myLabel.text = "test"
    }
}

答案 1 :(得分:0)

您看到消息的原因" 致命错误:在展开可选值时意外发现nil "是因为您正在尝试为nil的属性分配值。

在UITableViewDelegate方法tableView(_:didSelectRowAtIndexPath:)的实现中声明:

ViewController().myLabel.text = "test"

  1. 创建ViewController类的新实例(ViewController()所做的事)
  2. 访问新实例的myLabel属性(回想一下,在您将该属性定义为UILabel!的类的声明中,一个隐式解包的可选UILabel,以及隐式解包的选项开始生命为nil,直到它们被赋值())
  3. 尝试分配" test"到text的{​​{1}}属性 - 但正如我们刚才所说,myLabelmyLabel,这就是您收到错误消息的原因
  4. 修改

    努力帮助您达到规定的目标" 尝试更改UILabel的文字" (该标签是nil实例的属性myLabel),我建议的路线是:

    • 让您的ViewController班级采用ViewController协议
    • UITableViewDelegate课程中实施UITableViewDelegate协议方法,他们可以直接访问ViewController媒体资源

    完成此操作后,您对该委托方法的实现(现在位于myLabel类内)将如下所示:

    ViewController

    ......虽然这会有效,但即使这样也可以改进 - 我建议你查看Apple的Table View Programming Guide

    此外,除非有令人信服的理由不这样做,否则我还建议您将func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { myLabel.text = myTableView.dataSource.myArray[indexPath.row] } 班级作为ViewController - 从表格视图编程指南:" [t]创建表视图的类通常通过采用UITableViewDataSource和UITableViewDelegate协议使其自身成为数据源和委托 " (见section