如何将 2 组数据合并到一个 UITableview 中
Posted
技术标签:
【中文标题】如何将 2 组数据合并到一个 UITableview 中【英文标题】:How do I combine 2 sets of data into one UITableview 【发布时间】:2021-01-20 20:04:43 【问题描述】:我想知道如何将 2 组数据合并到一个表视图中
var myFirstData = ["Hello", "Hi"]
var mySecondData = ["Goodbye", "Bye"]
对于cellForRowAt
、numberOfRowsInSection
和trailingSwipeActionsConfigurationForRowAt
,我应该放什么来检测我正在使用哪个数组
如果可以的话,能给我一个示例项目吗
【问题讨论】:
数据源必须是一个数组。那么var dataArray = myFirstData + mySecondData
呢?
我建议您阅读有关使用 UITableView 的类似问题。也许看看什么是“委托/数据源”。简而言之,您需要实现这些方法,以便 UITableView 知道每个单元格(“行”)要显示什么以及有多少行等。
我不想合并数组
@AshwinPaudel 桌面视图还能如何工作?如果您担心var dataArray = myFirstData + mySecondData
,您的原始数组不会受到影响
@aheze 谢谢!!你能回答一下吗,我会标记它是正确的
【参考方案1】:
对于表格视图,您应该始终使用一组数据。这使得跟踪变得容易,因为您只需将请求的indexPath.row
处的对象返回到数据源即可。
如果您有 2 个要显示的数据数组,将它们组合成一个会容易得多...
var combinedData = myFirstData + mySecondData
...然后提供组合数组的tableview。您的原始数组将不受影响。
这是一个实现了cellForRowAt
、numberOfRowsInSection
和trailingSwipeActionsConfigurationForRowAt
的示例。
class TableCell: UITableViewCell
@IBOutlet weak var label: UILabel!
class ViewController: UIViewController
var myFirstData = ["Hello", "Hi"]
var mySecondData = ["Goodbye", "Bye"]
var combinedData = [String]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad()
super.viewDidLoad()
combinedData = myFirstData + mySecondData /// set combinedData (will not affect myFirstData or mySecondData)
tableView.dataSource = self
tableView.delegate = self
extension ViewController: UITableViewDelegate, UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return combinedData.count /// simply return the count of the entire combinedData array
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCellIdentifier", for: indexPath) as! TableCell
let textAtRow = combinedData[indexPath.row] /// also get what to display from combinedData
cell.label.text = textAtRow
return cell
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
let textAtRow = combinedData[indexPath.row]
let title = "Capitalize"
let action = UIContextualAction(style: .normal, title: title, handler: (_,_,_) in
let uppercasedText = textAtRow.uppercased()
/// Update data source when user taps action
self.combinedData[indexPath.row] = uppercasedText
/// if you want to modify the original arrays
if let firstIndex = self.myFirstData.firstIndex(of: textAtRow) /// get first index of text at this row, if exists
self.myFirstData[firstIndex] = uppercasedText /// set the text to the new text
if let firstIndex = self.mySecondData.firstIndex(of: textAtRow)
self.mySecondData[firstIndex] = uppercasedText
self.tableView.reloadRows(at: [indexPath], with: .automatic)
print("Combined: \(self.combinedData), myFirstData: \(self.myFirstData), mySecondData: \(self.mySecondData)")
)
action.image = UIImage(systemName: "textformat")
action.backgroundColor = .blue
let configuration = UISwipeActionsConfiguration(actions: [action])
return configuration
Repo on GitHub
【讨论】:
以上是关于如何将 2 组数据合并到一个 UITableview 中的主要内容,如果未能解决你的问题,请参考以下文章