ios - 在 Swift 中没有重复使用表格单元格的索引路径

标签 ios uitableview swift tableview

我注意到 Obj-C 中有很多关于此的问题,但我几乎不记得 Obj-C 并且每个答案都是针对该问题的。在这里我收到此错误:应用程序刷新时有时会出现“没有重复使用的表格单元格的索引路径”。我注意到当我不刷新但离开并重新打开表格 View 时,格式被破坏了。

这是我在几个地方使用的“刷新”方法:

   @IBAction func loadData(){
        timeLineData.removeAllObjects()
        //pulls the data from the server
        var findTimeLineData: PFQuery = PFQuery(className: "Sweets")
        findTimeLineData.findObjectsInBackgroundWithBlock{
            (objects:[AnyObject]!, error: NSError!) -> Void in
            if !error{
                for object:PFObject! in objects{
                    self.timeLineData.addObject(object)
                }
                let tempArray: NSArray = self.timeLineData.reverseObjectEnumerator().allObjects
                self.timeLineData = tempArray as NSMutableArray

                //reloads the data in the table view
                self.tableView.reloadData()
            }
        }
    }

和 TableView 方法:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
    let cell: SweetTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SweetTableViewCell

    let sweet: PFObject = self.timeLineData.objectAtIndex(indexPath.row) as PFObject

    //part of the animation
    cell.sweetTextView.alpha = 0
    cell.userNameLabel.alpha = 0
    cell.timestampLabel.alpha = 0


    cell.sweetTextView.text = sweet.objectForKey("content") as String

    //add the date
    var dateFormatter: NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "HH:mm yyyy-MM-dd"
    cell.timestampLabel.text = dateFormatter.stringFromDate(sweet.createdAt)

    //finds the sweeter associated with a pointer
    var findSweeter: PFQuery = PFUser.query()
    findSweeter.whereKey("objectId", equalTo: sweet.objectForKey("sweeter").objectId)

    findSweeter.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]!, error: NSError!)-> Void in
        if !error{
            let user: PFUser = (objects as NSArray).lastObject as PFUser
            cell.userNameLabel.text = user.username
        }

    }

    //adds animation
    UIView.animateWithDuration(1, animations: {
        cell.sweetTextView.alpha = 1
        cell.userNameLabel.alpha = 1
        cell.timestampLabel.alpha = 1
    })

    return cell
}

知道是什么导致了错误吗?

最佳答案

是什么导致了您的问题:当您滚动表格时,TableView 会使您对解析服务器的异步调用试图操作的单元格出队。

您可以通过以下方式解决此问题: 1- 在 Parse 的 Sweets 表中,将 PFUser 对象存储为指针 2- 在您的 loadData 函数中,通过 PFQuery 的 includeKey 方法从查询中获取用户

如果您根据此进行更改,则不必在每次调用 cellForRowAtIndexPath 时都查询 PFUser。

关于ios - 在 Swift 中没有重复使用表格单元格的索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467292/

相关文章:

ios - WKWebView 检查 HTML 表单提交

ios - 在 IOS 应用程序中随处使用对象

ios - 创建具有特定时间的 NSDate

错误按钮上的 iOS 标题

ios - SegmentedControl 隐藏和显示 TableView 单元格

ios - 将 UITableView 实例化为弹出窗口时,UITableViewCell 的属性为 nil

macos - 使用 swift 读取 CFDictionary 中的值

ios - 一直向下滚动到 UITableView 的底部

ios - UITextField.Text 从其他 View 返回 null

ios - 比特币 : GoogleMaps framework not found issue