【问题标题】:How to display dynamic data in ViewController in Swift如何在 Swift 中的 ViewController 中显示动态数据
【发布时间】:2016-10-10 13:12:24
【问题描述】:

Swift 我有Realm DB。在我的数据库中,我有来自服务器的 10 项数据(新闻标题)。现在我需要在ViewController 中动态显示来自Realm DB 的数据

【问题讨论】:

    标签: ios swift xcode realm


    【解决方案1】:

    根据你的代码

    for indexes in newsObj {
        newsLable.text = indexes.newsTitle
    }
    

    如果你的 newsObj 中有 10 条新闻,直到它只显示第 10 条新文本,如果你想显示 10 条新闻,那么

    写这个

    let indexes = newsObj[index]
    newsLable.text = indexes.newsTitle
    

    删除此代码

    for indexes in newsObj {
    newsLable.text = indexes.newsTitle
    }
    

    【讨论】:

      【解决方案2】:

      据我了解,您正在为每个索引调用以下方法:

      func elementAtScrollViewIndex(index: Int)
      

      所以对于每个索引,for 循环被调用了 10 次。因此,标签显示每个索引的第 10 条新闻的标题。

      只需去掉for循环,就可以做到以下几点:

      newsLable.text = newsObj[index].newsTitle
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-04
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-29
        相关资源
        最近更新 更多