在 Swift 中接收到新数据时返回新的 Cell
Posted
技术标签:
【中文标题】在 Swift 中接收到新数据时返回新的 Cell【英文标题】:Returning new Cell when new data is received in Swift 【发布时间】:2019-03-12 13:02:21 【问题描述】:所以我在我的应用程序中停下来了。我目前正在使用 tableview 来显示一些从 Arduino 发送的数据。现在我手动一次发送一个字节数组来模拟,但它最终会发送很多。目前,该应用程序可以很好地显示数据,就像我想要的那样,但是每次单击 Arduino 发送时,我都无法让它在新单元格中显示数据。
所以在numberOfRowsInSection
中它将返回 100 个相同数据的单元格。每次我从 Arduino 发送它时,我希望它返回 1 个单元格。因此,如果我单击发送 10 次,我想显示已发送数据的 10 个单元格。
目前我使用的是:return recievedBytes.count
,但这仅计算数组中的每个字节。但我想要一个新的单元格,每次都会收到一个新的字节数组。
有谁知道我需要返回什么才能做到这一点?
如果有任何不清楚的地方,请大声疾呼。
谢谢大家
这是表格视图代码:
func numberOfSections(in tableView: UITableView) -> Int
return 1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return 100 //THIS IS WHERE I NEED HELP :)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "RecieveCell", for: indexPath) as! RecieveTableCell
cell.rowNumber.text = "\(indexPath.row + 1)."
cell.modeLabel.text = "\(recievedModeType)"
cell.timeLabel.text = "\(String(message))μs"
return cell
编辑:
好吧,伙计们,我想我应该多写一些,因为我觉得我误导了你一点。我已经尝试了你的建议,但它并不完全是我的想法。我现在看到我写的有点误导。
例如。我从 Arduino 发送这个:[0x11,0x22,0x33,0x44,0x55,0x66,0x77]
在此我可以在表格视图中显示 MODE(recievedModeType) 和 TIME(message)。
按照你们的建议,我现在返回 7 个单元格,每个单元格中有一个元素。因为receivedBytes.count。跟我想的不太一样。
我想要的是通过发送该数组在一个单元格中显示模式和时间。只要它被发送,它将继续显示在更多的单元格中。所以从某种意义上说,如果要发送 50 个这样的数组,那么我希望有 50 个代表 MODE 和 TIME 的单元格。
但我现在会继续关注这个..
对于造成的混乱,我深表歉意。
谢谢!
【问题讨论】:
how to update data in tableview (Swift)的可能重复 欢迎来到 SO。一个简单的解决方案是将didSet
观察者添加到receivedBytes
数组。像这样:var recievedBytes = ["type of the data"]() didSet tableView.reloadData()
并在 numberOfRowsInSection
方法中保留 return recievedBytes.count
谢谢大家,这让我有了一些想法,太好了!让我试一试,我会告诉你进展如何!
【参考方案1】:
如果您想跟踪收到的数组,可以使用另一个数组
var receivedArrays: [[UInt8]] = []
var receivedBytes: [UInt8] = []
didSet
receivedArrays.append(receivedBytes)
tableView.reloadData()
然后您可以将 receivedCount 作为 numberOfRows 返回,并在 cellForRowAt 函数中根据需要使用您的接收字节数组。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return receivedArrays.count
【讨论】:
谢谢!现在我可以用数据返回一个单元格,但是当我再次尝试发送它时,它只是重新加载一个单元格。但我现在很接近了! 用一个单元格重新加载是什么意思?它不会向 tableView 添加新单元格? 我每次都覆盖了 recievedArray,所以难怪我只有一个单元格,我的 recieveBytes 函数中的一个简单的 += 运算符修复了它。您的回答帮帮我,谢谢!【参考方案2】:基本上,如果您打算用动态填充表格视图,我建议继续遵循return recievedBytes.count
的方法,这意味着继续使用recievedBytes
作为数据资源填充表格视图。
但这仅计算数组中的每个字节。但我想要一个新的细胞, 每次都会收到一个新的字节数组。
你可以做些什么来解决它:
更新recievedBytes
数组。
调用reloadData()
方法。
虽然我不知道recievedBytes
的确切类型是什么,但让我们考虑一下是[Int]
来查看示例:
class ViewController: UIViewController
// ...
var recievedBytes: [Int] = []
didSet
tableView.reloadData()
// ...
extension ViewController: UITableViewDataSource
// If the number of section should be 1, you don't have to implement numberOfSections
// and let it return 1, it is the default value for it.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return recievedBytes.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "RecieveCell", for: indexPath) as! RecieveTableCell
let currentByte = recievedBytes[indexPath.row]
cell.rowNumber.text = "\(indexPath.row + 1)."
cell.modeLabel.text = "\(currentByte)"
cell.timeLabel.text = "\(String(message))μs"
return cell
注意:
作为一种良好做法,对于这种情况,建议将 recievedBytes
声明为属性观察者,每次 recievedBytes
更新时,tableView.reloadData()
都会被调用。
recievedBytes
在处理cellForRowAt
方法时也应该是可靠的,currentByte
应该是recievedBytes
中基于当前行的字节,因此你可以显示它的值。
【讨论】:
var receivedBytes: [UInt8] 用于我得到acual消息的函数中,在这种情况下它是以μs为单位的时间。但是我会看看你写的这个,谢谢! @Wolfgang 很高兴为您提供帮助。如果我的回答回答了您的问题,请不要忘记接受它;)以上是关于在 Swift 中接收到新数据时返回新的 Cell的主要内容,如果未能解决你的问题,请参考以下文章
Swift:使用按钮导航到新的 ViewController
创建一个viewcontroller并将现有VC的标签和按钮添加到新的VC Swift iOS
以编程方式从 collectionView Cell didSelectItemAt 推送到新的 ViewController - swift
将 UIView 移动到新的超级视图并从 UITableViewCell 再次返回