向下滚动后,UITableView在不同的部分重复自定义单元格和UIView

时间:2016-03-17 03:15:09

标签: ios swift uiview uitableview

我有一个ChartCell(UITableViewCell),它有一个用于显示图表的UIView

class ChartCell: UITableViewCell {

    @IBOutlet weak var itemChart: ChartView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state

    }

}

但我的UITableView在向下滚动后重复单元格

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return items.count
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        return items[section]["title"].stringValue
    }

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        let header = view as! UITableViewHeaderFooterView
        header.textLabel?.font = UIFont(name: (header.textLabel?.font?.fontName)!, size: 13)!
    }

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return 30.0
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.items[section]["data"].count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        switch items[indexPath.section]["cell"]{
        case "bar" :

            let cell = tableView.dequeueReusableCellWithIdentifier("bar", forIndexPath: indexPath) as! ChartCell
            cell.itemChart.data = items[indexPath.section]["data"][indexPath.row]
            cell.itemChart.total = items[indexPath.section]["total"].intValue
            return cell

        default :
             let cell = tableView.dequeueReusableCellWithIdentifier("issue", forIndexPath: indexPath) as! IssueCell
            cell.itemDescription.text = items[indexPath.section]["data"][indexPath.row]["group_name"].stringValue
            cell.itemAmount.text = items[indexPath.section]["data"][indexPath.row]["ticket_count"].stringValue
            return cell
        }

    }

    func loadReport(sender : AnyObject) {

        let closedController = self
        self.activityIndicatorView.startAnimating()

        Services.getClosedReport({(status : Bool, data : JSON) -> Void in

            let tR = data["data"]["summary_by_resolution"]["data"] ?? []
            let tP = data["data"]["summary_by_priority"]["data"] ?? []
            let tI = data["data"]["summary_by_issue_type"]["data"] ?? []

            var totalR = 0

            for (_, subJson) in tR {
                if totalR < subJson["ticket_count"].intValue{
                    totalR = subJson["ticket_count"].intValue
                }
            }

            var totalP = 0

            for (_, subJson) in tP {
                if totalP < subJson["ticket_count"].intValue{
                    totalP = subJson["ticket_count"].intValue
                }
            }


            let tS = [
                JSON(["title" : "By Top 5 Resolution Types", "data" : tR , "cell" : "bar", "total" : JSON(totalR)]),
                JSON(["title" : "By Top 5 Issue Types", "data" : tI, "cell" : "issue"]),
                JSON(["title" : "By Priority", "data" : tP , "cell" : "bar", "total" : JSON(totalP)]),

            ]

            closedController.items = JSON(tS)
            self.itemReportTable.reloadData()
            self.activityIndicatorView.stopAnimating()
        })
    }

我为cellForRowAtIndexPath调试了tableView函数但是每个部分和行中的数据都是正确的,单元格没有将UIView的drawRect函数调用到自定义单元格中,最后一个在另一个部分中重复另一个单元格< / p>

更新: Custom UIView如下:

class ChartView: UIView {

    var data : JSON!
    var total : Int!

    override func drawRect(rect: CGRect) {

        let itemView = self
        let data  = itemView.data

        let corePoint = CGPoint(x: 10, y: 10)
        let viewWidth = self.bounds.width
        let space = CGFloat(10.0)

        // MARK : -- Description

        let widthL = CGFloat(100.0)
        let heightL = CGFloat(20.0)
        let text = data["group_name"].stringValue

        let rectL = CGRectMake(corePoint.x, corePoint.y, widthL, heightL)
        let itemL = UILabel(frame: rectL)
        itemL.text = text
        itemL.font = UIFont(name: itemL.font.fontName, size: 13.0)
        itemView.addSubview(itemL)


        // MARK : -- CGRect

        let heightR = CGFloat(30.0)
        let toY = (heightR - heightL) / 2.0

        let yR = corePoint.y - toY
        let xR = corePoint.x + widthL + space
        let rect = CGRectMake(xR, yR, 0, heightR)
        let itemR = UIView(frame: rect)
        itemR.backgroundColor = UIColor(red: 49.0/255.0, green: 149.0/255, blue: 169.0/255.0, alpha: 1.0)
        itemView.addSubview(itemR)


        // MARK : -- Amount

        let widthA = widthL
        let heightA = heightL
        let textA = data["ticket_count"].stringValue

        let xA = xR + space
        let yA = corePoint.y

        let rectA = CGRectMake(xA, yA, widthA, heightA)
        let itemA = UILabel(frame: rectA)
        itemA.text = textA
        itemA.font = UIFont(name: itemA.font.fontName, size: 13.0)
        itemView.addSubview(itemA)


        // MARK : -- Animation


        UIView.animateWithDuration(0.75, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in

            let widthT = (viewWidth - xR - 50) * CGFloat(data["ticket_count"].intValue) / CGFloat(self.total)

            itemR.frame = CGRectOffset(CGRectMake(xR, yR, widthT, heightR), 0.0, 0.0)
            itemA.frame.origin.x = xA + widthT + 5.0

        }, completion: nil)

    }
}

0 个答案:

没有答案