来自Swift的Custom Data Cell

时间:2015-04-21 23:05:18

标签: ios iphone uitableview swift

我是Swift和IOS的新手,我曾经有一个普通的表视图,一切正常。我现在已经实现了一个自定义表视图单元格,并想知道如何使用我的UITableView实现我的PrepareForSegue方法。我希望能够将选定的Table Cell索引发送到segue,以便下一个控制器访问某个阵列位置。现在发送者Object是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()
        }
    }






}

2 个答案:

答案 0 :(得分: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单元格中。

如果你想要这样做,你必须设置一个单元格的属性(例如:rowIndex:Int),它指示此方法中数据的indexRow&f; func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath :NSIndexPath)&#39;。您的代码如下:

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

答案 1 :(得分:-1)

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

这样你可以使用通过故事板创建的通用segue,在选定的函数上实现表格单元格。