Swift UITableView 上的条件数据打印

Swift conditional data printing on UITableView

我有一个 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 传递数据,但我只成功地从我的数据库而不是所选行的数据,如下所示:

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但没有成功。

你的方法有很多问题。

对于初学者,您不应该在 cellForRowAtIndexPath 方法之外调用 dequeueReusableCellWithIdentifier

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

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

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

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

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

我认为您的目标视图控制器不会受到影响,因为您这样做:

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

此条件始终为假。 删除此条件,它应该正常工作。

注意:参见 Duncan C post。