swift uitableviewCell重复子视图

时间:2015-04-27 17:13:23

标签: uitableview repeat reusability addsubview

我在与目标关联的uitableviewcell中有一个UIButton:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! ProCell
    cell.btn.tag = indexPath.row
        cell.btn.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
    return cell
}

这是buttonClicked函数:

func buttonClicked(sender:UIButton) {
    let boton = sender as UIButton
    let index = boton.tag

    let buttonPosition: CGPoint = boton.convertPoint(CGPointZero, toView:   self.tableView)
    var indexPath: NSIndexPath =  self.tableView.indexPathForRowAtPoint(buttonPosition)!
    let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! ProductoCell
cell.btn.setImage(UIImage (named: "rotacion-Null"), forState: UIControlState.Normal)
    let indicador = UIActivityIndicatorView()
    indicador.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
    indicador.color = UIColor(red: 153.0/255.0, green: 0.0, blue: 0.0, alpha: 1.0)
    indicador.startAnimating()
    cell.btn.addSubview(indicador)



    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        let datosEd = ed.search(product)

        dispatch_async(dispatch_get_main_queue(), {
            //update UI in main thread once the loading is completed.

            indicador.stopAnimating()
            indicador.hidesWhenStopped = true


            let label2 = UILabel(frame: CGRect(x: 15  , y: -5, width: 16, height: 16))
            label2.textAlignment = NSTextAlignment.Center
            label2.font = UIFont.boldSystemFontOfSize(13.0)
            label2.backgroundColor = UIColor (red: 0.85, green: 0.20, blue: 0.33, alpha: 1.0)
            label2.textColor = UIColor.whiteColor()
            label2.layer.masksToBounds = true
            label2.layer.cornerRadius = 8
            let rowData = datosEd[0] as! NSDictionary
            if rowData["art"] as! String == "No Results" {
                label2.text = "0"
                cell.contentView.addSubview(label2)
                cell.btn.setImage(UIImage (named: "ed-25G"), forState: UIControlState.Normal)
            } else {
                label2.text = String(datosEd.count)
                cell.btn.addSubview(label2)
                cell.btn.setImage(UIImage (named: "ed-25R"), forState: UIControlState.Normal)
            }
        });
    })

}

问题是,当我向下滚动并再次向上滚动时,子视图(label2)会在某些单元格中重复出现。

阅读一些回复认为它与细胞的重用有关,但我不知道如何使子视图不再重复。

请帮助一下......: - )

0 个答案:

没有答案