当我插入新记录时,UITableview中不会显示

时间:2018-05-28 11:24:20

标签: ios swift uitableview swift4

我创建了一个填充了虚拟数据的联系人应用程序,当我尝试插入新的联系人时没有出现在我的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.
        }
    }


}

Screenshot

1 个答案:

答案 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.sectionindexPath.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()
        }
    }
}