简体   繁体   English

TableView单元内的事件性能更好

[英]Better performance events inside TableView cell

I am developing a Facebook like application, where if the post is too long, it gets cut down and at the end of it "See more" appears that has some click events. 我正在开发类似Facebook的应用程序,如果帖子过长,它将被删减,并在其末尾出现“查看更多”消息,其中包含一些点击事件。 (I use FRHyperLabel for it) (我使用FRHyperLabel

Everything works fine, but I've got a question: If the formatting+event is decleared inside tableView: cellForRowAt indexPath: is it bad for the performance? 一切正常,但是我有一个问题:如果在tableView: cellForRowAt indexPath:取消了formatting + event tableView: cellForRowAt indexPath:是否对性能不利?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            var message : message
            var cell : MessageCell

            let message = self.messages[indexPath.row]

            // World Message

            cell = tableView.dequeueReusableCell(withIdentifier: "essageCell", for: indexPath) as! MessageCell

            cell.messageLabel.attributedText = message.limitMessageMax250char()

            //Add link if needed
            if message.count > appSettings.maxCharacters.message {

                let handler = {
                    (hyperLabel: FRHyperLabel!, range: NSRange!) -> Void in

                    self.alert(title: "Test", message: "Alert: because clicked on link")
                }

                let text = cell.messageLabel.text!
                let nsRange = text.calculateUnicodeScalar(start: text.unicodeScalars.count-8, len: 8) // See more is 8 characters

                cell.messageLabel.setLinkFor(nsRange, withLinkHandler: handler)
            }

            return cell

    }

Or should I do it in the Cell's file, when it's awakeFromNib + with delegate 还是在awakeFromNib +有委托的情况下,在Cell的文件中执行此操作

IMO you don't want to do this in the for cellForRowAt call mostly because you want to make that call return quickly and also because you are only returning the formatted cell. IMO,您不想在for cellForRowAt调用中执行此操作,主要是因为您想使该调用快速返回,并且还因为您只返回格式化的单元格。

I would argue/recommend that you set your handler for the row when it is being initialized and that way the handler value is only being set once. 我认为/建议您在初始化该行时为该行设置处理程序,这样该处理程序值将仅被设置一次。

Remember that cellForRowAt is called multiple times as the user scrolls through and as cells come into view. 请记住,在用户滚动浏览和进入单元格时会多次调用cellForRowAt。

review the https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614861-tableview for more information as to how cells are reused. 查看https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614861-tableview ,以获取有关如何重复使用单元格的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM