Swift新手:如何更新detailTextLabel?.text

时间:2015-03-26 06:25:53

标签: ios swift

我是Swift的新手,正在开展一个小项目。 我有两个UITableViewController:TableViewController和DetailViewController。

单击TableViewController单元格将导航到DetailViewController。 单击DetailViewController单元格将导航回TableViewController并使用所选项目更新detailTextLabel?.text。

现在的问题是我不知道如何更新detailTextLabel?.text。 我可以将所选数据从DetailViewController传递给TableViewController,但不知道如何更新单元格。

class TableViewController: UITableViewController,DataSelectedDelegate{
    var sessions=[Session]()

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

        self.sessions = [Session(Game: "Game", Detail: "SuperTrack"),Session(Game: "Type",Detail : "Race")]


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func userSelectedInformatiom(info:NSString){
        //cell.detailTextLabel?.text=info

    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sessions.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        var session:Session
        session=sessions[indexPath.row]
        cell.textLabel?.text = session.Game

        cell.detailTextLabel?.text = sessions[indexPath.row].Detail
        return cell

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail"{
            let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
            detailVC.delegate=self
        }

    }


}

DetailViewController:

import UIKit

protocol DataSelectedDelegate{
    func userSelectedInformatiom(info:NSString)

}
class DetailViewController: UITableViewController {

        var types=[GameType]()
        var test:String!

        var delegate:DataSelectedDelegate? = nil

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



            self.types = [GameType(Type:"Race"),GameType(Type:"Shoot"),GameType(Type:"Kid"),GameType(Type:"Adult")]
        }

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

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

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            //let identifier: String = "tableCell"
            let cell = self.tableView.dequeueReusableCellWithIdentifier("Cells", forIndexPath: indexPath) as UITableViewCell

            var type:GameType
            type=types[indexPath.row]
            cell.textLabel?.text = type.Type
            return cell
        }

        override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            //println("You selected cell #\(indexPath.row)!")

            //let selectedCell = tableView.cellForRowAtIndexPath(indexPath)
            //selectedCell?.accessoryType = .Checkmark
            if (delegate != nil){
                let selectedIndex:NSString=types[indexPath.row].Type
                delegate!.userSelectedInformatiom(selectedIndex)
                self.navigationController?.popViewControllerAnimated(true)
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

有多种可能的解决方案:

  1. 您可以将选定的单元格传递给prepareForSegue中的detailViewController(sender对象是抽头单元格)。然后,您可以在委托电话中传回该信息。
  2. 您可以通过userSelectedInformatiom检查委托函数self.tableView.indexPathForSelectedRow()中所选行的indexPath。使用该indexPath,您可以通过cellForRowAtIndexPath
  3. 获取单元格
  4. 您可以在执行segue之前存储对所选单元格的引用
  5. ...