I was wondering why this code says index out of range
Specific problem code:
cell.textLabel?.text = contactsAry[indexPath.row]
Whole code having issues:
extension ContactsViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (myContact.count + contactsAry.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < myContact.count {
let contact = myContact[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "MainUsersAccount") as!
ContactsLayout
cell.setContacts(contacts: contact)
return cell
} else {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "UserItem")
cell.textLabel?.text = contactsAry[indexPath.row]
return cell
}
}
This is my array "contactsAry" let contactsAry = ["Brandon", "Luisa"]
MainUserAccount setting code:
func createArray() -> [Contacts] {
var tempContact: [Contacts] = []
let userName = "Temp User"
let myContact = Contacts(UserIcon: UIImage(systemName: "\(userName.prefix(1).lowercased()).circle.fill")!, UserName: userName, MyAccountLabel: "My Account")
tempContact.append(myContact)
return tempContact
}
I have two different prototype cells trying to be added to the same tableView. The first cell which is basically just a users account cell is identified as "MainUserAccount" and it's just one cell that can have it's icon and name changed. The second cell is identified as "UserItem" which is basically just a list of users that you have added to your "friends list". Im unsure why I keep getting this error. Any help would be greatly appreciated.
Full error code I get in console:
Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444 2020-08-02 03:04:18.896635-0700 Schedule[44839:2131710] Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
What you can do is to remove the count of precious array ... otherwise it will try to access 3rd instance rather then 0
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "UserItem")
cell.textLabel?.text = contactsAry[indexPath.row - myContact.count]