如何重新加载 table 视图? Xcode 11
How can I reload a table view? Xcode 11
我目前正在尝试向tableView中添加一行,我添加视图的代码如下
@IBAction func done(segue:UIStoryboardSegue) {
let newContact = segue.source as! NewContactViewController
FirstName = newContact.firstName
LastName = newContact.lastName
print("Adding the name: " + FirstName + " " + LastName + " to the nameArry")
nameArry.append(FirstName + " " + LastName)
print("Added " + nameArry[nameArry.count - 1] + " to name array")
let indexPath = IndexPath(row: myContact.count + contacts.count - 1, section: 0) // Set the indexPath to the lowest part of the array
tableView.beginUpdates() // Starts the update
tableView.insertRows(at: [indexPath], with: .automatic) // Insert row at the lowest part of the table
tableView.endUpdates() // Ends the update
newContact.firstName = "" // Clears the variable "firstName"
newContact.lastName = "" // Clears the variable "lastName"
view.endEditing(true) // Disables Editing, saw it on youtube.
}
我使用 myContact.count + contacts.count
作为 indexPath,因为那是我在 tableView 上使用的
// Get Number of items in table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (myContact.count + contacts.count)
}
我收到以下错误:
Thread 1: Exception: "attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update"
你为什么要删除一个?
let indexPath = IndexPath(row: myContact.count + contacts.count - 1, section: 0)
.....写这一行不减1这样...
let indexPath = IndexPath(row: myContact.count + contacts.count , section: 0)
因为如果您要添加一行并从总数中减去它...那么更新后现有部分中包含的行数等于更新前该部分中包含的行数
我明白了,我没有得到准确的计数,因为我没有向联系人数组附加任何内容。
contacts.append(Contacts(MyContact: nameArry[nameArry.count - 1], Me: ""))
let indexPath = IndexPath(row: myContact.count + contacts.count - 1, section: 0)
我目前正在尝试向tableView中添加一行,我添加视图的代码如下
@IBAction func done(segue:UIStoryboardSegue) {
let newContact = segue.source as! NewContactViewController
FirstName = newContact.firstName
LastName = newContact.lastName
print("Adding the name: " + FirstName + " " + LastName + " to the nameArry")
nameArry.append(FirstName + " " + LastName)
print("Added " + nameArry[nameArry.count - 1] + " to name array")
let indexPath = IndexPath(row: myContact.count + contacts.count - 1, section: 0) // Set the indexPath to the lowest part of the array
tableView.beginUpdates() // Starts the update
tableView.insertRows(at: [indexPath], with: .automatic) // Insert row at the lowest part of the table
tableView.endUpdates() // Ends the update
newContact.firstName = "" // Clears the variable "firstName"
newContact.lastName = "" // Clears the variable "lastName"
view.endEditing(true) // Disables Editing, saw it on youtube.
}
我使用 myContact.count + contacts.count
作为 indexPath,因为那是我在 tableView 上使用的
// Get Number of items in table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (myContact.count + contacts.count)
}
我收到以下错误:
Thread 1: Exception: "attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update"
你为什么要删除一个?
let indexPath = IndexPath(row: myContact.count + contacts.count - 1, section: 0)
.....写这一行不减1这样...
let indexPath = IndexPath(row: myContact.count + contacts.count , section: 0)
因为如果您要添加一行并从总数中减去它...那么更新后现有部分中包含的行数等于更新前该部分中包含的行数
我明白了,我没有得到准确的计数,因为我没有向联系人数组附加任何内容。
contacts.append(Contacts(MyContact: nameArry[nameArry.count - 1], Me: ""))
let indexPath = IndexPath(row: myContact.count + contacts.count - 1, section: 0)