首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableAttributedString未正确呈现

NSMutableAttributedString未正确呈现
EN

Stack Overflow用户
提问于 2019-08-17 23:59:15
回答 1查看 158关注 0票数 0

我使用的是一个SplitViewController;主节点是具有一系列RightDetail单元格的tableView,这些单元格的属性字符串是通过值设置弹出窗口的回调来设置的。

通过设置单元格的detailTextLabel的方法传回和设置的值。它在DispatchQueue.main.async中执行此操作,因为对detailTextLabel的某些更新是通过来自Network.Framework的调用完成的

奇怪的是,当弹出窗口进行更改时,对属性字符串的更改不会正确显示。在正确绘制之前,焦点必须切换到下一个表单元格。

因此,只有当焦点更改为"Flow Rate“时,才能正确渲染温度,如blow所示。

我已经单步执行了以下代码,并检查是否向self.buttonCell?.detailTextLabel?.attributedText写入了正确的值

您还会看到我添加了setNeedsLayout()setNeedsFocusUpdate()

代码语言:javascript
复制
func setDetailValueWithUnit( to index: Int) {
    /** sets the detailtext to the value as the specified index and appends units.
        it does this within the main dispatchQueue as this method is called by
        operationResult which is called by the statemachine.
     **/
    DispatchQueue.main.async {
        let font = self.buttonCell?.detailTextLabel?.font
        let finalString = NSMutableAttributedString(string: valueString,attributes: [.font:font!])

        if let units = self.units() {
            finalString.append(units)
            self.buttonCell?.detailTextLabel?.attributedText = finalString

            //next 2 lines experimental in an attempt to fix the attributed text not
            //being consistently redendered correctly
            self.buttonCell?.detailTextLabel?.setNeedsLayout()
            self.buttonCell?.detailTextLabel?.setNeedsFocusUpdate()
            NSLog("setDetailValueWithUnit: %d", index)
            return
        } else {
            assert(false, "No Units found")
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-08-18 00:15:41

问题是你所做的是完全错误的。您永远不应该触摸单元格的文本标签字体、文本或任何其他直接类似的内容。这不是表视图的工作方式。您应该设置表的数据模型并从那里更新表,从而导致对可见单元格调用cellForRowAt:。这样,只有cellForRowAt:配置单元,一切都会很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57537822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档