如何将 2 组数据组合成一个 UITableview
How do I combine 2 sets of data into one UITableview
我想知道如何将 2 组数据合并为一个 table 视图
var myFirstData = ["Hello", "Hi"]
var mySecondData = ["Goodbye", "Bye"]
对于 cellForRowAt
、numberOfRowsInSection
和 trailingSwipeActionsConfigurationForRowAt
我应该放什么来检测我使用的是哪个数组
如果可以,能给我一个示例项目吗
您应该始终为 table 视图使用一组数据。这使得跟踪变得容易,因为您可以 return 请求 indexPath.row
处的对象到数据源。
如果您有 2 个数据数组要显示,将它们合并为一个数组会容易得多...
var combinedData = myFirstData + mySecondData
...然后将组合数组输入 table 视图。您的原始数组将不受影响。
这是实施了 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
}
}
我想知道如何将 2 组数据合并为一个 table 视图
var myFirstData = ["Hello", "Hi"]
var mySecondData = ["Goodbye", "Bye"]
对于 cellForRowAt
、numberOfRowsInSection
和 trailingSwipeActionsConfigurationForRowAt
我应该放什么来检测我使用的是哪个数组
如果可以,能给我一个示例项目吗
您应该始终为 table 视图使用一组数据。这使得跟踪变得容易,因为您可以 return 请求 indexPath.row
处的对象到数据源。
如果您有 2 个数据数组要显示,将它们合并为一个数组会容易得多...
var combinedData = myFirstData + mySecondData
...然后将组合数组输入 table 视图。您的原始数组将不受影响。
这是实施了 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
}
}