UITableView上的Swift条件数据打印

时间:2015-06-29 11:45:26

标签: ios uitableview row segue

我有一个UITableView,这是它的cellForRowAtIndexPath及其numberOfRowsInSection:

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

    var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customTableViewCell") as! UITableViewCell
    let task = frc.objectAtIndexPath(indexPath) as! Task

        cell.textLabel?.text = task.summary
        var detail = task.detail
        var context = task.context
        var due = task.date
        var status = task.status
        var responsible = task.responsable
        var folder = task.folder

        cell.detailTextLabel?.text = "Contexte: \(context), Detail: \(detail), Status: \(status), Ending date: \(due)"

    return cell
}

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

    let numberOfRowsInSection = frc.sections?[section].numberOfObjects
    return numberOfRowsInSection!

}

我想要做的是,当我点击一行时,它会打开该行的详细视图,因此我尝试使用prepareForSegue传递数据,但我只能成功发送来自segue的数据我的数据库而不是所选行的数据,如下所示:

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



    if let identifier = segue.identifier{

        //On vérifie que l'identifier est le bon, permet d'envoyer qu'à la View qu'on veut si a le risque d'envoyer à plusieurs. Si on veut envoyer ailleurs, il suffit de créer la vue en question et de rajouter un "case" avec le nom du nouvel identifier.
        switch identifier {
            case "Show Detail":
                //Creation du lien vers la base SQLite


                let entityDescription = NSEntityDescription.entityForName("Task", inManagedObjectContext: self.context!)
                let request = NSFetchRequest()
                request.entity = entityDescription
                var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customTableViewCell") as! UITableViewCell

                var error: NSError?
                var objects = self.context?.executeFetchRequest(request, error: &error)
                let match = objects![0] as! NSManagedObject

                let editTaskVC = segue.destinationViewController as! EditTaskViewController
                if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell){

                        editTaskVC.Name = match.valueForKey("summary") as! String
                        editTaskVC.Detail = match.valueForKey("detail") as! String
                        editTaskVC.Status = match.valueForKey("status") as! String
                        editTaskVC.Owner = match.valueForKey("responsable") as! String
                        editTaskVC.Context = match.valueForKey("context") as! String
                        editTaskVC.IdValue = match.valueForKey("id") as? String
                        editTaskVC.Field = match.valueForKey("folder") as! String
                        let dateFormatter = NSDateFormatter()
                        dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
                        let date = dateFormatter.dateFromString(match.valueForKey("date") as! String)
                        editTaskVC.EndDatePicker?.date = date!

                }
            default: break
        }
    }

}

我尝试做的是从刚刚单击的行而不是数据库发送到destinationViewController数据,如下所示:

editTaskVC.Name = cell.textLabel?.text

我在网上搜索并看到了一些解决方案,例如使用didSelectRowAtIndexPath但没有成功。

2 个答案:

答案 0 :(得分:1)

您的方法存在许多问题。

对于初学者,您不应在dequeueReusableCellWithIdentifier方法之外致电cellForRowAtIndexPath

接下来,你的基本方法是错误的。

视图对象不存储状态,它们显示它并从用户收集输入。表格视图单元格可以随时滚动离开屏幕,它的当前设置将丢失。当用户更改单元格的值时,您需要将其保存到存储数据当前状态的模型对象。

通常这意味着保存对数据库的更改,如果这是您用作模型的内容。如果希望更改在用户单击保存按钮之前保持挂起状态,或者由于某些其他原因尚未准备好将更改保存到数据库,则需要将更改保存到特定于表视图的某些数据模型。由视图控制器管理的数组适用于此。

在任何情况下,当用户点击一个单元格时,您应该查看表格视图的数据,以便将数据传递给详细控制器,而不是尝试从单元格中读取数据。

您要做的是将tapped cell的indexPath保存到实例变量。然后在prepareForSegue中,如果您发现这是由用户点击单元格触发的segue,请查看该indexPath并获取该indexPath的相应数据以传递给下一个视图控制器。

答案 1 :(得分:1)

我认为您的目标视图控制器不会受到影响,因为您执行了以下操作:

if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell){
    //code
}

这种情况总是错误的。 删除这个条件,通常它应该工作。

注意:见Duncan C post。