在编辑模式下(在UITableView中)如何执行不同的segue?

时间:2015-11-30 09:50:18

标签: ios swift uitableview segue

我正在使用UITableView创建一个简单的iPhone应用程序。我正在使用默认的MasterDetail应用程序模板。现在(在编辑模式下)当我按下任何表格单元格时没有任何反应。但是,当我处于正常模式时,会启动细节segue。如何覆盖编辑模式,以便我启动自定义segue以转到其他UIViewController。

P.S。:我仍然希望保留继承删除功能。

这是我在MasterViewController中的代码:

class MasterViewController: UITableViewController {
let kFileName: String = "/resolutionData.plist"
var resolutions = [Dictionary<String,String>]()
var achievedResolutions = [Dictionary<String,String>]()
// TO DO create a class to get this array
let iconArray = ["Fish","Fly","Heart","HelpingHand","Melon","Star","Tentacles","Volunteering"]

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

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

    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
    self.navigationItem.rightBarButtonItem = addButton


    //extract the path
    NSLog(dataFilePath())
    /**
    *   Check where is the sandbox of the application
    *   and if there is read from the data file and story it to "objects" array
    */
    if NSFileManager.defaultManager().fileExistsAtPath(dataFilePath()){
        var temp = NSArray(contentsOfFile: dataFilePath()) as! [Dictionary<String,String>]

        for res in temp{
            if res["isAchieved"] == "Y"{
                achievedResolutions.append(res)
            }else{
                resolutions.append(res)
            }
        }

        //... if there is not - create it
    } else {
        let data =  [["name":"Resolution name test","startingDate":"24-11-15","achievingDate":"01-01-2016","icon":iconArray[0],"isAchieved":"N"] as NSDictionary] as NSArray
        //if the file does not exist...
        if !NSFileManager.defaultManager().fileExistsAtPath(dataFilePath()){
            //... create it
            NSFileManager.defaultManager().createFileAtPath(dataFilePath(), contents: nil, attributes: nil)
        }
        //write to it
        data.writeToFile(dataFilePath(), atomically: true)
    }

}

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

func insertNewObject(sender: AnyObject) {
    performSegueWithIdentifier("editDetails", sender: sender)
}

func saveDateToFile(){
    let data =  resolutions as NSArray
    data.writeToFile(dataFilePath(), atomically: true)
}

func notifyTableViewForNewInsertion(){
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

}

// MARK: - Segues

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let object = resolutions[indexPath.row] as Dictionary
        (segue.destinationViewController as! DetailViewController).detailItem = object
        }
    }

}

// MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Active"
    }else{
        return "Achieved"
    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return resolutions.count
    }else{
        // TODO replace that with an actual array count
        return achievedResolutions.count
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    if indexPath.section == 0{
        let object: AnyObject? = resolutions[indexPath.row] ["name"]
        cell.textLabel!.text = object as? String
        cell.detailTextLabel!.text = resolutions[indexPath.row]["achievingDate"]
        cell.imageView!.image = UIImage(named: resolutions[indexPath.row]["icon"]!)
    } else {
        let object: AnyObject? = achievedResolutions[indexPath.row] ["name"]
        cell.textLabel!.text = object as? String
        cell.detailTextLabel!.text = resolutions[indexPath.row]["achievingDate"]
        cell.imageView!.image = UIImage(named: resolutions[indexPath.row]["icon"]!)
    }
    return cell
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        resolutions.removeAtIndex(indexPath.row)
        saveDateToFile()
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}


func dataFilePath() -> String{
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

    //get the first path and convert it to str
    let docDicrectyory: String = paths[0] as! String

    return  "\(docDicrectyory)\(kFileName)"

}

}

3 个答案:

答案 0 :(得分:2)

不是直接从故事板中执行您的segue,而是添加UITableViewDelegate方法:

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

    if tableView.editing {
        performSegueWithIdentifier("id_Segue_Editing_VC", sender: tableView.cellForRowAtIndexPath(indexPath))
    } else {
        performSegueWithIdentifier("id_Segue_Standard_VC", sender: tableView.cellForRowAtIndexPath(indexPath))
    }
}

sender设置为所选单元格 - 这与默认的UIKit行为相匹配。

然后在prepareForSegue方法中,您可以根据segue标识符向视图控制器添加自定义值。

答案 1 :(得分:1)

覆盖willBeginEditingRowAtIndexPath:功能。这将在开始编辑之前调用。在那里你可以初始化一个全局变量。

@property(nonatomic, String) BOOL editing

并在

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

   if (editing) {

       if([[segue identifier] isEqualToString:@"identifierOne"]){


       }else if([[segue identifier] isEqualToString:@"identifierTwo"]){

       }
   }    
}

答案 2 :(得分:1)

有一个用于此的表格视图属性:

self.tableView.allowsSelectionDuringEditing = YES;