自定义单元格重复

Custom cells repeating

我有一个 table 视图嵌入 ViewController 的应用程序,每当我导航到另一个 ViewController 并导航回 table 视图时,滚动时单元格重复。有没有人对如何防止这种情况有任何建议? table视图的当前代码是:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return marathonRaces.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let singleCell: marathonTableViewCell = tableView.dequeueReusableCellWithIdentifier("marathonCell") as! marathonTableViewCell

    singleCell.marathonName.text = marathonRaces[indexPath.row]
    singleCell.entry.text = "\(entryNumber[indexPath.row])"
    singleCell.entries.text = "\(entires[indexPath.row])"
    singleCell.length.text = "\(length[indexPath.row])"

    return singleCell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let indexPath = self.marathonsTableView.indexPathForSelectedRow!

    let currentCell = marathonsTableView.cellForRowAtIndexPath(indexPath) as! marathonTableViewCell

    let marathonEvents = currentCell.marathonName.text

    self.performSegueWithIdentifier("marathonDetail", sender: self)
}

我正在使用 swift、xcode7 并作为我的后端进行解析

viewDidAppear 中唯一相关的代码是:

var query = PFQuery(className: “Marathons")
    query.orderByAscending("end")
    query.findObjectsInBackgroundWithBlock { (marathons, error:NSError?) -> Void in
        if(error == nil ){
        //success

            for marathon in marathons! {
            self.marathonRaces
          .append(marathon[“marathonName"] as! String)
            self.entry.append(marathon[“entryNumber"] as! Int)
            self.entries.append(marathon[“entries"] as! Int)
            self.length.append(marathon[“length"] as! Int)



            }

            self.marathonsTableView.reloadData()

        }else {
        print(error)

        }
    }

您是否检查过数据源 marathonRaces 是否获得了更多条目?

您可能会在每次后退导航中添加更多条目,如果是这样,要么不添加它们,要么在添加它们之前删除所有条目。

问题出在您的 viewDidAppear 方法中。每次出现控制器时,您都会从后台获取数据,将它们附加到您的数组并重新加载表格视图。例如,将用于获取数据的代码移动到 viewDidLoad 并且 "repeating" 应该消失。