【问题标题】:Table view Help…..selected cells from one table view passed to another table view swift表格视图帮助......从一个表格视图中选择的单元格快速传递到另一个表格视图
【发布时间】:2015-06-30 00:39:53
【问题描述】:

这是一个两部分的问题...

  1. 我的问题是....弄清楚如何在第一个表格视图中选择选定的单元格...(来自解析的 pfobjects)保存到一个数组中,然后能够填充那些选定的单元格另一个 tableview 控制器上的单元格....

  2. 如何将第一个表格视图的选定单元格存储到另一个表格视图的一个表格视图单元格中,然后在其上显示一个显示指示器,打开以查看所有添加的单元格?抱歉,这比要更改或添加的具体代码更具概念性……这是我到目前为止所拥有的…… 提前感谢朋友们。

    override func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
    
        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PFTableViewCell!
        if cell == nil {
           cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellIdentifier")
        }
        // Extract values from the PFObject to display in the table cell
    
        if let customerFirstName = object?["customerName"] as? String {
           cell?.textLabel?.text = customerFirstName
        }
    
       if let customerStreetAddress = object?["customerStreetAddress"] as? String {
           cell?.detailTextLabel?.text = customerStreetAddress
       }
    
       if let indexPaths = tableView.indexPathsForSelectedRows() {
          for var i = 0; i < indexPaths.count; ++i {
    
             var thisPath = (indexPaths as! [NSIndexPath])[i]
             var cell = tableView.cellForRowAtIndexPath(thisPath)
             if let cell = cell {
    
               self.addedPoolarray.append(cell)
               // Do something with the cell
              // If it's a custom cell, downcast to the proper type
             }
       }
    }
    
    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "toRoutesVc" {
            let destination = segue.destinationViewController as! UITableViewController
    
        }
     }
    

【问题讨论】:

    标签: ios swift uitableview parse-platform


    【解决方案1】:

    删除这些代码:

    if let indexPaths = tableView.indexPathsForSelectedRows() {
       for var i = 0; i < indexPaths.count; ++i {
    
          var thisPath = (indexPaths as! [NSIndexPath])[i]
          var cell = tableView.cellForRowAtIndexPath(thisPath)
          if let cell = cell {
    
            self.addedPoolarray.append(cell)
            // Do something with the cell
            // If it's a custom cell, downcast to the proper type
          }
    }
    

    这是不正确的。 UITableView 重复使用单元格,这意味着将使用不同的PFObject 和不同的 indexPath 再次运行一个单元格。

    相反,您可以在 prepareForSegue 方法中将这些选定的单元格填充到另一个 tableview 控制器上:

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "toRoutesVc" {
            let destination = segue.destinationViewController as! RoutesVC
    
            let indexPaths = self.tableView.indexPathsForSelectedRows()
    
            /* 
               If the first table view controller 
               and the another table view controller have the same data source, 
               simply pass the indexPaths 
            */
            // destination.selectedObjects = indexPaths
    
            /* Or pass the PFObjects: */
            var selectedObjects = [PFObject]()
            for indexPath in indexPaths {
                selectedObjects.append(objects[indexPath.row])
            }
            destination.selectedObjects = selectedObjects
        }
    }
    

    您需要使用从第一个表视图接收的数组属性创建一个自定义控制器(UITableViewController 的子类)。

    class RoutesVC: UITableViewController {
    
        var selectedObjects: [AnyObject]?
    
        ...
    }
    

    【讨论】:

    • 感谢您花时间写出来!欣赏它!刚到家,明天就试一试:)!!
    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多