在不替换行的情况下插入行中的行 -

发布于 2025-02-09 00:14:44 字数 495 浏览 2 评论 0原文

在我的应用中,我有一个从firebase提取的数据。我有一个UitaiteViewController,并且想连续插入应用程序中的文本。 就是这样(请原谅不良的例子,但我不能

数据拉力

太多

细节

介绍

。 :JKL

第5行:Mno

我想实现的目标:

第1:ABC

第2行:DEF

ROW 3:APP

ROW 4:GHI

ROW 5:JKL

ROW 6:来自应用程序的文本

第7行:MNO

如何实现这一目标?我试图在cellforrowat中做这样的事情,

if indexPath.row % 3 == 0 {
cell.text = "custom text"
}

但这正在替换每个第三行内容。可以这么说,我想介于两者之间。

In my app I have a data pull from Firebase. I have a UITableViewController and would like to insert in a row a text from within the app. The data pull would be like this (please excuse the bad example but I cannot go into too much detail ..)

The original data pull:

Row 1: abc

Row 2: def

Row 3: ghi

Row 4: jkl

Row 5: mno

What I would like to achieve:

Row 1: abc

Row 2: def

Row 3: text from the app

Row 4: ghi

Row 5: jkl

Row 6: text from the app

Row 7: mno

How can I achieve this? I was trying to do something like this in cellForRowAt

if indexPath.row % 3 == 0 {
cell.text = "custom text"
}

But this is replacing every 3rd rows content. I would like to put a row in between, so to speak.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

≈。彩虹 2025-02-16 00:14:44

您可以使用本地数据修改服务器数据。

var serverData = ["a","b","c","d","e","f","g","h","i","j","k","l","m"]
let localAppData = ["1","2","3","4","5","6","7","8","9","10"]

var modified = [String]()
var counter = 0
for index in 1...serverData.count {
    let value = serverData[index - 1]
    if index % 3 == 0 && index != 0 {
        if counter < localAppData.count {
            modified.append(localAppData[counter])
        }else{
            modified.append(value)
        }
        counter += 1
    }else{
        modified.append(value)
    }
}

serverData.removeAll()
serverData.append(contentsOf: modified)
print(serverData) //["a", "b", "1", "d", "e", "2", "g", "h", "3", "j", "k", "4", "m"]

if counter < localAppData.count {
    // Appeds the remain local data to your serverData
    serverData.append(contentsOf: localAppData[counter...localAppData.count-1])
}
print(serverData) //["a", "b", "1", "d", "e", "2", "g", "h", "3", "j", "k", "4", "m", "5", "6", "7", "8", "9", "10"]

注意:修改后,您必须重新加载tableView

You can modify your server data with local data.

var serverData = ["a","b","c","d","e","f","g","h","i","j","k","l","m"]
let localAppData = ["1","2","3","4","5","6","7","8","9","10"]

var modified = [String]()
var counter = 0
for index in 1...serverData.count {
    let value = serverData[index - 1]
    if index % 3 == 0 && index != 0 {
        if counter < localAppData.count {
            modified.append(localAppData[counter])
        }else{
            modified.append(value)
        }
        counter += 1
    }else{
        modified.append(value)
    }
}

serverData.removeAll()
serverData.append(contentsOf: modified)
print(serverData) //["a", "b", "1", "d", "e", "2", "g", "h", "3", "j", "k", "4", "m"]

if counter < localAppData.count {
    // Appeds the remain local data to your serverData
    serverData.append(contentsOf: localAppData[counter...localAppData.count-1])
}
print(serverData) //["a", "b", "1", "d", "e", "2", "g", "h", "3", "j", "k", "4", "m", "5", "6", "7", "8", "9", "10"]

Note: After modification you have to reload the tableView

风尘浪孓 2025-02-16 00:14:44

您可以通过在第三位置插入值并在CellForrowat中使用该数据源来更新数据源

var a = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
         var temp = a
        for (ind, _) in a.enumerated() {
            if ind % 3 == 0 && ind != 0 {
                temp.insert("current text", at: ind)
            }
        }
print(temp) // Prints ["a", "b", "c", "current text", "d", "e", "current text", "f", "g", "h", "i"]

You can update the datasource by inserting the value at 3rd position and use that datasource in cellforrowat

var a = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
         var temp = a
        for (ind, _) in a.enumerated() {
            if ind % 3 == 0 && ind != 0 {
                temp.insert("current text", at: ind)
            }
        }
print(temp) // Prints ["a", "b", "c", "current text", "d", "e", "current text", "f", "g", "h", "i"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文