我创建了一个填充了虚拟数据的联系人应用程序,当我尝试插入新的联系人时没有出现在我的UITableView中但是如果我打印出来我的数组显示我就在那里。 我的删除也不是很好。是删除整个部分而不是删除我选择的行。 你能帮我修一下插入功能和删除功能吗? 感谢。
这是我的代码:
class Contact {
var fullName: String
var phoneNumber: String?
init(fullName: String, phoneNumber: String) {
self.fullName = fullName
self.phoneNumber = phoneNumber
}
}
var contactsArray = [Contact]()
var sections = [[Contact]]()
// Logic functionality for my "Contact" application
class ContactViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var nameTextField: UITextField!
@IBOutlet var phoneTextField: UITextField!
@IBOutlet var contactsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
contactsTableView.delegate = self
contactsTableView.dataSource = self
phoneTextField.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
contactsTableView.reloadData()
}
}
extension ContactViewController: UITableViewDelegate, UITableViewDataSource{
// Add a new contact to the end of list
@IBAction func insertNewContact(_ sender: UIButton) {
if nameTextField.text != nil && nameTextField.text != "" {
contactsArray.append(Contact(fullName: nameTextField.text!, phoneNumber: phoneTextField.text!))
contactsTableView.reloadData()
}
}
// Return no of sections from your list
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
// Return no of rows in each section from your list
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].count
}
// Insert a custom cell in your table view
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let contact = sections[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
cell.configContact(contact)
return cell
}
// Delete a section when you swipe from right to left
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
sections.remove(at: indexPath.row)
contactsTableView.reloadData() // xCode is confused sometimes and because my outlet var was named "tableView" and not "contactsTableView" it was trying to reload data from the parameter "tableView" of function and delete wasn't working at all. Now is deleting the whole section but I still need to work on it.
}
}
}
答案 0 :(得分:1)
您的数据包含在contactsArray
中,但您的表格数据源是sections
数组。更改sections
时,您需要更新contactsArray
数组。
将创建sections
数组的代码移动到自己的方法中。在将数据插入contactsArray
之后和在表格上调用reloadData()
之前调用它:
func createSectionsArray() {
let firstLetters = contactsArray.map { $0.titleFirstLetter }
let uniqueFirstLetters = Array(Set(firstLetters))
sortedFirstLetters = uniqueFirstLetters.sorted()
sections = sortedFirstLetters.map { firstLetter in
return contactsArray
.filter { $0.titleFirstLetter == firstLetter }
.sorted { $0.fullName < $1.fullName }
}
}
您还希望从viewDidLoad()
调用它来代替您删除的代码来创建该功能。
<强>删除:强>
要删除,请先使Contact
课程符合Equatable
:
class Contact: Equatable {
static func == (lhs: Contact, rhs: Contact) -> Bool {
return lhs.fullName == rhs.fullName && lhs.phoneNumber == rhs.phoneNumber
}
var fullName: String
var phoneNumber: String?
init(fullName: String, phoneNumber: String) {
self.fullName = fullName
self.phoneNumber = phoneNumber
}
}
然后,当项目被删除时,使用indexPath.section
和indexPath.row
查找contact
,找到contact
中的contactsArray
并将其删除,重新生成sections
数组,然后重新加载表:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let contact = sections[indexPath.section][indexPath.row]
if let index = contactsArray.index(of: contact) {
contactsArray.remove(at: index)
createSectionsArray()
contactsTableView.reloadData()
}
}
}