ios - 当我插入一条新记录时,UITableview 中没有显示

标签 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

最佳答案

您的数据包含在 contactsArray 中, 但您的表的数据源是 sections大批。您需要更新 sections contactsArray 时的数组被改变了。

移动创建 sections 的代码数组到它自己的方法中。 contactsArray插入数据后调用在调用 reloadData() 之前在 table 上:

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 , 找到 contactcontactsArray并删除它,重新生成 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()
        }
    }
}

关于ios - 当我插入一条新记录时,UITableview 中没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50565291/

相关文章:

ios - 只能通过wifi连接到CloudKit的公共(public)容器,但不能通过蜂窝连接

ios - 如何解决: Unable to simultaneously satisfy constraints

ios - 调整 UICollectionView 的大小以使其内容适合 AutoLayout View

uitableview - 为带有部分的表格 View 实现搜索栏和搜索显示

ios - 更改 UIView 边框的形状

ios - 应用程序因 AVAudioSession privateBeginInterruption 而崩溃

ios - 在游戏结束场景中使用 View Controller

iOS Google map sdk 方法 "idleAtCameraPosition"未被调用

iOS 14 WidgetKit : How to create different layouts for every widget size family?

iphone - 如何在iphone中单击时更改tableView Cell按钮