UITextView,在UITableViewCell中,在iOS 7 +中使用autoLayout

时间:2015-06-30 07:26:58

标签: ios uitableview autolayout uitextview nsattributedstring

好吧,我想我已经阅读了所有与我一样的问题。

我在UITableViewCell中有一个UITextView,单元格本身包含一个imageView,一个UILabel作为标题,UITextView和另一个UILabel。

截图: enter image description here

我认为限制是正确的。目前视图在大多数情况下进行的估计是正确的,但在我的情况下,内容大于UITableViewCell的高度,这可能是由于UITableViewCell的延迟渲染,导致其他列表项在此后面绘制(仅限于此出现在最上面。

估算高度的代码:

func heightForCellAtIndexPath(indexPath:NSIndexPath) -> CGFloat {
        if (calculatorCell[cellIdentifier] == nil) {
            calculatorCell[cellIdentifier] = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
        }

        if let cell = calculatorCell[cellIdentifier] {
            populateCell(cell, indexPath: indexPath)

            cell.setNeedsLayout()
            cell.layoutIfNeeded()

            return cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        }
        ErrorClass.log("Could not instantiate cell? returning 100 float value instead for listView")
        return 100.0
    }

这是一个通用函数,因为它处理UITableViewCell而不是自定义,所以在这种情况下populateCell,第一个索引返回带有问题的单元格,其余的是正常工作的注释,我只是分享这一切,因为人们会要求它:

if (indexPath.row == 0) {
        let cellUnwrapped = cell as! ChallengeHeader

        cellUnwrapped.setContent(
            challenge!.content,
            date: StrFormat.deadLineFromDate(challenge!.endAt),
            likes: Language.countValue("challenge_count_like", count: challenge!.totalLikes),
            likeController: self,
            commentCount: Language.countValue("global_count_comment", count: challenge!.totalComments)
        )

        cellUnwrapped.like(userLiked)

        cellUnwrapped.loadAttachments(challenge!.attachments)

        return cellUnwrapped
    } else {
        let cellUnwrapped = cell as! ListItemComment
        let challengeComment = items[(indexPath.row - 1)] as! ChallengeComment
        var username = "Anonymous"
        if let user = challengeComment.author as User? {
            cellUnwrapped.iconLoad(user.avatar)
            username = user.displayName
        }

        cellUnwrapped.setContentValues(
            StrFormat.cleanHtml(challengeComment.text),
            date: StrFormat.dateToShort(challengeComment.createdAt),
            username: username
        )

        return cellUnwrapped
}

单元格的setContentvalues将html字符串解析为NSAttributedString,它包含在if中以防止重新加载以重新呈现NSAttributedString。它不会改变,但渲染确实需要一段时间:

func setContent(content:String, date:String, likes:String, likeController : LikeController, commentCount:String) {
    if (content != txtContent) {
        self.contentLabel.text = nil
        self.contentLabel.font = nil
        self.contentLabel.textColor = nil
        self.contentLabel.textAlignment = NSTextAlignment.Left
        contentLabel.attributedText = StrFormat.fromHtml(content)
        txtContent = content
    }
    deadlineLabel.text = date
    likesLabel.text = likes
    self.likeController = likeController
    likeButton.addTarget(self, action: "likeClicked:", forControlEvents: .TouchUpInside)
    totalComments.text = commentCount
    totalComments.textColor = StyleClass.getColor("primaryColor")
    doLikeLayout()
}

所以TL; DR;我在UITableViewCell内部有一个UITextView缩放大小,使用autolayout并包含一个NSAttributedString来解析html和html链接。为什么它不能很好地呈现,以及如何解决它?

0 个答案:

没有答案