表视图单元格内容重复当我重新加载数据时

时间:2016-03-25 01:39:30

标签: ios uitableview refresh reloaddata

在等待重新加载数据时滚动时,我的表格视图单元格正在重复。重新加载后,重置所有项目并删除所有重复内容。这里有一个显示问题的视频,任何人都知道为什么以及如何修复它?正如你在视频中看到的那样,hdsdjhjdhhsdsjshhsdhsjhdjd在顶部再次重复? https://www.dropbox.com/s/e0arcajkuuot8xa/Reflector%20Recording.mp4?dl=0

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

    let cell:StatusTableCell! = tableView.dequeueReusableCellWithIdentifier("statusCell", forIndexPath: indexPath) as! StatusTableCell


    if (cell == nil) {
        cell.uploadDate = nil
        cell.uploadStatus.text = nil
        cell.statusUploader.setTitle("", forState: .Normal)
    }


    cell.photoImage.hidden = true
    self.photoHidden = true

    print("\n\n\n\n")


    cell.layoutIfNeeded()

    cell.likesButton.setImage(UIImage(named: "Likes"), forState: .Normal)
    cell.likesButton.setTitle("0", forState: UIControlState.Normal)

    print("\n\n\n\n")

    //Makes The Cell Of Text View Detect Link
    cell.uploadStatus.dataDetectorTypes = .Link
    cell.uploadStatus.delegate = self

    do {


        print(date2)
        cell.statusUploader.setTitle("\(user2[indexPath.row])", forState: UIControlState.Normal)
        if isLoadingLikes == false {

            //Modified Height Based ON Status NOTE: STILL BUGGY
            cell.uploadStatus.text = status2[indexPath.row]

            cell.uploadStatus.frame.size.height = textViewHeight(cell.uploadStatus)

            test = cell.uploadStatus.frame.size.height

            self.tableView(tableView, heightForRowAtIndexPath: indexPath)

            //Likes Button Data
            let uploadLikes = likes2[indexPath.row]
            cell.likesButton.tag = indexPath.row
            cell.likesButton.setTitle("\(uploadLikes)", forState: UIControlState.Normal)

            //Add An Action Connector To Likes Button
            cell.likesButton.addTarget(self, action: Selector("likesFunction:"), forControlEvents: .TouchUpInside)
            cell.moreButton.addTarget(self, action: Selector("moreFunction:"), forControlEvents: .TouchUpInside)



            print(liked2)
            if liked2[indexPath.row] == "liked" {
                cell.likesButton.setImage(UIImage(named: "Likes2"), forState: .Normal)

            }else {

                cell.likesButton.setImage(UIImage(named: "Likes"), forState: .Normal)

            }

        }

        print("----------------------------- Row \(indexPath.row)")
        print("Checking Date Array\(date2[indexPath.row])")
        print("Check NSDATE \(NSDate().timeIntervalSinceDate(date2[indexPath.row]))")
        print("Checking TIME FORMATTER \(timeAgoSinceDate(date2[indexPath.row], numericDates: true))")
        print("Status = \(status2[indexPath.row])")


        cell.uploadDate.text = timeAgoSinceDate(date2[indexPath.row], numericDates: true)
        let stringWithNSDataDetector: String = cell.uploadStatus.text
        let error: NSError? = nil
        let dataDetector: NSDataDetector = try NSDataDetector(types: NSTextCheckingType.Link.rawValue)

        //Check if (error) before
        var allMatches: [AnyObject] = [AnyObject]()
        dataDetector.enumerateMatchesInString(stringWithNSDataDetector, options: NSMatchingOptions.Anchored, range: NSMakeRange(0, stringWithNSDataDetector.characters.count), usingBlock: { (match:NSTextCheckingResult?, flags:NSMatchingFlags, pointer:UnsafeMutablePointer<ObjCBool>) -> Void in

            print("Link \(match!.resultType == .Link)")

            if match!.resultType == .Link {
                allMatches.append(match!.URL!)
                self.photoHidden = false
                cell.photoImage.hidden = false
                print(match!.URL!)
            }
        })



        print("ALL Matches \(allMatches)")

        print("Test \(test) -- row : \(indexPath.row)")

        print(cell.uploadStatus.frame.size.height)

    }
    catch {

        print("Error Loading Status -- (StatusTableView)")

    }

    print("----------------------------- End \n")

    cell.setNeedsDisplay()
    return cell
}

这是另一个

   func textViewHeight (textView:UITextView) -> CGFloat{

    textView.scrollEnabled = false
    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame;

    return textView.frame.height

}


internal func textViewHeightForText(text: String?, andWidth width: CGFloat) -> CGFloat {
    let calculationView = UITextView()
    //IMPORTANT - have to specify the name/size of your font
    let attributedText = NSAttributedString(string: (text != nil) ? text! : "", attributes: [NSFontAttributeName : UIFont(name: "Helvetica Neue", size: 16.0)!])
    calculationView.attributedText = attributedText
    let height = calculationView.sizeThatFits(CGSize(width: width, height: CGFloat.max)).height
    return height
}


override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let width = tableView.frame.size.width - 79

    if photoHidden == false {
        return textViewHeightForText(status2[indexPath.row], andWidth: width) + 260


    }else {
        return textViewHeightForText(status2[indexPath.row], andWidth: width) + 140
    }
}

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {




    let cell = tableView.dequeueReusableCellWithIdentifier("statusCell", forIndexPath: indexPath) as! StatusTableCell

    cell.uploadStatus.text = status2[indexPath.row]

    cell.uploadStatus.frame.size.height = textViewHeight(cell.uploadStatus)

    test = cell.uploadStatus.frame.size.height

    self.tableView(tableView, heightForRowAtIndexPath: indexPath)






    //If it is last row of the loaded table views , it will loading it got more than one current object else finish loading
    if (indexPath.row == objectCount - 1) && isLoadingMoreCell == false
    {
        self.isLoadingMoreCell = true
        self.tableView.tableFooterView = footerView
        footerLoading.center.x = self.footerView.frame.width/2
        footerLoading.startAnimating()
        loadMoreCell()


    }
}

0 个答案:

没有答案