gpt4 book ai didi

ios - 快速使用数据库中的动态内容更新静态数组

转载 作者:行者123 更新时间:2023-11-29 05:38:30 25 4
gpt4 key购买 nike

我正在尝试实现 TimelineTableViewCell ( https://github.com/kf99916/TimelineTableViewCell/blob/master/TimelineTableViewCellDemo/TimelineTableViewController.swift )。它在时间轴中显示内容。

对于时间轴帖子,演示具有如下静态内容:

  // TimelinePoint, Timeline back color, title, description, lineInfo, thumbnail, illustration
let data:[Int: [(TimelinePoint, UIColor, String, String, String?, String?, String?)]] = [0:[
(TimelinePoint(), UIColor.black, "12:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", nil, nil, "Sun"),
(TimelinePoint(), UIColor.clear, "19:00", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", nil, nil, "Moon")
], 1:[
(TimelinePoint(), UIColor.lightGray, "08:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "60 mins", nil, "Sun"),
(TimelinePoint(), UIColor.lightGray, "09:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "30 mins", nil, "Sun")]]

它可以很好地处理静态内容。

现在,我正在尝试使用 TableView 中数据库的内容更改这些静态内容。我有点困惑如何声明该数据数组以及稍后将数据附加到其中。

我的获取数据函数如下所示:

          {
dataHelper.getConditionsData { (data1) in
for post in data1 {
let post1 = post as! NSDictionary
let headline = post1.value(forKey: "headline")
let text1 = post1.value(forKey: "text")
let t_date = post1.value("t_date")
//how do i declare empty array at top and append these to data array?
}

我这样做的方式是给我的数据没有附加成员。

有什么帮助吗?如何为顶部的元组声明空的二维数组并将其附加到数据库中的数据?

编辑:对于数据库,我正在这样做。

   var i=0; 
dataHelper.getConditionsData { (data1) in
for post in data1
{
let postA = post as! NSDictionary
let condition_text = postA.value(forKey: "condition_text")
let condition_date = postA.value(forKey: "condition_date")
self.data[i] = [ (TimelinePoint(), UIColor.lightGray, condition_date as!
String,condition_text as! String , "60 mins", nil, "Sun") ]
i = i+1
}
}

最佳答案

编辑:

OP问题的实际答案是:

检索数据后,您没有调用 reloadData()

<小时/>

您可以将 data 字典声明为:

var data:[Int: [(TimelinePoint, UIColor, String, String, String?, String?, String?)]] =
[Int: [(TimelinePoint, UIColor, String, String, String?, String?, String?)]]()

(注意:为了便于阅读,我将其分成两行)

然后,当你想添加到字典中时,你可以这样做:

    data[0] = [
(TimelinePoint(), UIColor.lightGray, "08:30", "This is the Start of a test.", "60 mins", nil, "Sun"),
(TimelinePoint(), UIColor.lightGray, "09:30", "This is the End of the test.", "30 mins", nil, "Sun")
]

用您的标题text1等变量替换“字符串”。

<小时/>

编辑:我克隆了您引用的TimelineTableViewCell项目。

在包含的演示中的 TimelineTableViewController.swift 中,我:

  • 创建了空的data字典,如我所示
  • 注释掉let data:示例数据
  • viewDidLoad() 中添加了两个示例数据元素

所以,它看起来像这样:

class TimelineTableViewController: UITableViewController {

var data:[Int: [(TimelinePoint, UIColor, String, String, String?, String?, String?)]] =
[Int: [(TimelinePoint, UIColor, String, String, String?, String?, String?)]]()

// TimelinePoint, Timeline back color, title, description, lineInfo, thumbnail, illustration
// var data:[Int: [(TimelinePoint, UIColor, String, String, String?, String?, String?)]] = [0:[
// (TimelinePoint(), UIColor.black, "12:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", nil, nil, "Sun"),
// (TimelinePoint(), UIColor.black, "15:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", nil, nil, "Sun"),
// (TimelinePoint(color: UIColor.green, filled: true), UIColor.green, "16:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "150 mins", "Apple", "Sun"),
// (TimelinePoint(), UIColor.clear, "19:00", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", nil, nil, "Moon")
// ], 1:[
// (TimelinePoint(), UIColor.lightGray, "08:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "60 mins", nil, "Sun"),
// (TimelinePoint(), UIColor.lightGray, "09:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "30 mins", nil, "Sun"),
// (TimelinePoint(), UIColor.lightGray, "10:00", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "90 mins", nil, "Sun"),
// (TimelinePoint(), UIColor.lightGray, "11:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "60 mins", nil, "Sun"),
// (TimelinePoint(), UIColor.lightGray, "12:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "30 mins", "Apple", "Sun"),
// (TimelinePoint(), UIColor.lightGray, "13:00", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "120 mins", "Apple", "Sun"),
// (TimelinePoint(), UIColor.lightGray, "15:00", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "150 mins", "Apple", "Sun"),
// (TimelinePoint(), UIColor.lightGray, "17:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "60 mins", nil, "Sun"),
// (TimelinePoint(), UIColor.lightGray, "18:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "60 mins", nil, "Moon"),
// (TimelinePoint(), UIColor.lightGray, "19:30", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "30 mins", nil, "Moon"),
// (TimelinePoint(), backColor: UIColor.clear, "20:00", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", nil, nil, "Moon")
// ]]

override func viewDidLoad() {
super.viewDidLoad()

data[0] = [
(TimelinePoint(), UIColor.lightGray, "08:30", "This is the Start of a test.", "60 mins", nil, "Sun"),
(TimelinePoint(), UIColor.lightGray, "09:30", "This is the End of the test.", "30 mins", nil, "Sun")
]

data[1] = [
(TimelinePoint(), UIColor.lightGray, "16:30", "This is the Start of another test.", "60 mins", nil, "Mon"),
(TimelinePoint(), UIColor.lightGray, "17:30", "This is the End of the second test.", "30 mins", nil, "Mon")
]

let timelineTableViewCellNib = UINib(nibName: "TimelineTableViewCell", bundle: Bundle(for: TimelineTableViewCell.self))
self.tableView.register(timelineTableViewCellNib, forCellReuseIdentifier: "TimelineTableViewCell")
}

// didn't touch anything after here

运行应用程序现在会显示两个“天”,每个“天”有两个条目:

enter image description here

如果这不是你得到的,那么你就犯了其他错误......

<小时/>

关于ios - 快速使用数据库中的动态内容更新静态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56794186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com