簡體   English   中英

文本在iOS的uitableview單元格中重疊

[英]Text getting overlapped in uitableview cells in iOS swift

我在情節UITableViewCell板上創建了一個原型UITableViewCell ,其中包含1個ImageView,3個用於顯示內容的標簽。 所有控件在UITableViewCell相應子類中都有插座。 我顯示的消息可以是多行。 所以我必須添加以下方法

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

 func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

標簽的文本越來越重疊,每當我滾動tableview時都會發生。 它僅發生在前兩行或最后一行。但是當我在google上搜索時,我發現它可以隨時發生在任何行上。 這是我通常編寫的擴展代碼。

extension ConversationViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let count = selectedConversation?.msgArrayWithBodyText.count {
            return count
        }
        return 0
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier(conversationMessageIdentifier, forIndexPath: indexPath) as! ConversationMessageTableViewCell
        let currMsg =  selectedConversation.msgArrayWithBodyText[indexPath.row]
        cell.userFullNameLabel.text = currMsg.senderName
        cell.dateLabel.text = selectedConversation.getShortDateForMessage(currMsg.timeSent)
        cell.messageBodyLabel.text = currMsg.bodyText //Constants.testString

        let imageUrl = "\(Constants.serverUrl)/api/users/\(currMsg.senderId)/profilePicture"
        if let item = ImageCache.sharedInstance.objectForKey(imageUrl) as? CacheableItem {
            print("\(imageUrl) found in cache, expiresAt: \(item.expiresAt)")
            cell.userImageView.image = UIImage(data: item)
        } else {
            cell.userImageView.image = UIImage(named: "Profile")

            self.imageDownLoadingQueue.addOperationWithBlock({
                if let url = NSURL(string: imageUrl) {
                    if let purgableData = NSPurgeableData(contentsOfURL: url) {
                        if let downloadedImage = UIImage(data: purgableData) {
                            print("cache: \(imageUrl)")

                            let item = CacheableItem()
                            item.setData(purgableData)
                            ImageCache.sharedInstance.setObject(item, forKey: imageUrl)

                            NSOperationQueue.mainQueue().addOperationWithBlock({
                                if let updateCell = tableView.cellForRowAtIndexPath(indexPath) as? ConversationMessageTableViewCell {
                                    updateCell.userImageView.image = downloadedImage
                                }
                            })
                        }
                    }
                }
            })
        }
        //cell.clipsToBounds = true
        return cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if cell.respondsToSelector(Selector("setSeparatorInset:")) {
            cell.separatorInset = UIEdgeInsetsZero
        }
        if cell.respondsToSelector(Selector("setLayoutMargins:")) {
            cell.layoutMargins = UIEdgeInsetsZero
        }
        if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
            cell.preservesSuperviewLayoutMargins = false
        }

        cell.backgroundColor = UIColor.clearColor()
        cell.backgroundView = UIView(frame:cell.frame)
        if(cell.backgroundView?.layer.sublayers?.count > 1){
            cell.backgroundView?.layer.sublayers?.removeAtIndex(0)
        }
        let layer = self.getGradientLayer(indexPath.row)
        cell.backgroundView?.layer.insertSublayer(layer, atIndex: 0)

    }
}

這是我為原型單元格編寫的UITableViewCell的子類。

class ConversationMessageTableViewCell: UITableViewCell {
    @IBOutlet weak var userImageView: UIImageView!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var userFullNameLabel: UILabel!
    @IBOutlet weak var messageBodyLabel: UILabel!

    @IBOutlet weak var messageBodyLabelHeightConstraint: NSLayoutConstraint!

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

        userImageView.layer.borderColor = UIColor.clearColor().CGColor
        userImageView.layer.borderWidth = 1.0
        userImageView.layer.cornerRadius = (userImageView.frame.height/2)
        userImageView.clipsToBounds = true
    }

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

        // Configure the view for the selected state
    }

這是編輯過的屏幕截圖(我已經塗黑了名字和面孔)

這是怎么回事

需要幫助!

我可以看到長文本不會被弄亂,但是只有小文本才會發生。

基本上,這僅在我上下滾動tableview幾次時才會發生。 加載tableview時,所有文本均會正確顯示,但隨后會混亂,然后清除一段時間,然后再次混亂。

我找到了解決此問題的方法。 故事板上有一個選項,內容為“清除圖形上下文”,它基本上可以在重畫標簽之前清除標簽的圖形。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM