【问题标题】:Segue from Custom Data Cell in Swift从 Swift 中的自定义数据单元开始
【发布时间】:2015-04-21 23:05:18
【问题描述】:

我是 Swift 和 IOS 的新手,我曾经有一个普通的表格视图,一切正常。我现在已经实现了一个自定义表格视图单元格,并且想知道如何使用我的 UITableView 实现我的 PrepareForSegue 方法。我希望能够将选定的表格单元索引发送到 segue,以便下一个控制器访问某个数组位置。现在发送者对象是一个 CustomCell: UITableViewCell 对象。我可以从该对象或其他方式访问表索引吗?

//
//  ViewController.swift
//  OBU Bus Tracker
//
//  Created by AJ Norton on 4/20/15.
//  Copyright (c) 2015 AJ Norton. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate {

    @IBOutlet var table: UITableView!

    var locations = [String]()
    var overall = Dictionary<String, AnyObject>()

    override func viewDidLoad() {
        super.viewDidLoad()



        // check if the user is running the app for the first time
        //        if let firstTime = NSUserDefaults.standardUserDefaults().objectForKey("firstTime")
        //        {
        //
        //            println("second time")
        //        }
        //        else
        //        {
        //            println("worked")
        //            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstTime")
        //        }

        if let path = NSBundle.mainBundle().pathForResource("busSchedule", ofType: "plist")
        {
            if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject>
            {
                locations = dict.keys.array
                overall = dict
            }
        }
        // Do any additional setup after loading the view, typically from a nib.
    }

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


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {

        return locations.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell: CustomRouteViewCell = table.dequeueReusableCellWithIdentifier("Cell") as! CustomRouteViewCell

        cell.locationTitle.text = "\(locations[indexPath.row])"


        return cell
    }



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        println("I was clicked")
        performSegueWithIdentifier("routeToTime", sender: indexPath.row)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        //TODO
        println("\(sender?.integerValue)")
        if segue.identifier == "routeToTime"
        {
            var ttvc =  segue.destinationViewController as! TimeViewController
            var s = sender as! CustomRouteViewCell
            println("\(s.in)")
            var place = s.indentationLevel as! Int
            var dicts = overall[locations[place]] as! Dictionary<String,AnyObject>
            var arr = dicts["Weekday"] as! [Int]

            ttvc.days = dicts
            ttvc.times = arr.reverse()
        }
    }






}

【问题讨论】:

  • 你的segue是用cell还是controller做的?如果发件人是单元格,这听起来像是来自单元格,但在这种情况下,您不应该在 didSelectRowAtIndexPath 中调用 performSegue(实际上您根本不需要实现该方法)。
  • 那是未知的我不知道segue是在哪里制作的。在我的 prepareForSegue 方法中不再调用它。我不知道在哪里调用了 segue。
  • 你没有做segue吗?制作时是从单元格中拖动还是从控制器图标中拖动?

标签: ios iphone uitableview swift


【解决方案1】:
 performSegueWithIdentifier("routeToTime", sender: indexPath.row)

你调用上面的方法。所以在这个方法中:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

本地参数 sender 是 indexPath.row,不是 CustomRouteViewCell 的实例。

这样你就可以写代码了:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "routeToTime"
    {
        var ttvc =  segue.destinationViewController as! TimeViewController
        var place = sender as! Int
        var dicts = overall[locations[place]] as! Dictionary<String,AnyObject>
        var arr = dicts["Weekday"] as! [Int]

        ttvc.days = dicts
        ttvc.times = arr.reverse()
    }
}

最好在 MVC 模式中编写代码,因此不要将数据存储在 tableview 单元格中。

无论你想做什么,你都必须在这个方法'func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)'中设置一个单元格的属性(例如:rowIndex: Int)来表示数据的indexRow。您的代码如下所示:

if segue.identifier == "routeToTime"
    {
        var ttvc =  segue.destinationViewController as! TimeViewController
        var cell = sender as! CustomRouteViewCell
        var place = cell.indexRow
        var dicts = overall[locations[place]] as! Dictionary<String,AnyObject>
        var arr = dicts["Weekday"] as! [Int]

        ttvc.days = dicts
        ttvc.times = arr.reverse()
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell: CustomRouteViewCell = table.dequeueReusableCellWithIdentifier("Cell") as! CustomRouteViewCell

    cell.locationTitle.text = "\(locations[indexPath.row])"
    cell.indexRow = ... //Set the index of cell's data.

    return cell
}

顺便说一下,indentationLevel 属性是:

var indentationLevel: Int // 调整内容缩进。默认为 0

【讨论】:

    【解决方案2】:

    如何将单元格索引存储为 NSUserDefault 并从另一端检索它?

    这样您就可以使用通过情节提要创建的通用转场,在所选功能上实现表格单元格。

    【讨论】:

    • 没有 NSdefault 这样的东西。如果您的意思是 NSUserDefaults,那是个坏主意;那不是他们的目的。 OP 应该学习如何在 prepareForSegue 中正确传递数据,而不是使用 NSUserDefaults 作为拐杖。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多