I have a problems with compiler my project UITableViewCell - swift

When I build code it is fine. But after a run, I have 2 errors:
Thread 1: signal SIGABRT
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath as IndexPath) as! customTableViewCell
cell.transAmount?.text = "cell number \(indexPath.row)."
cell.transDate?.text = "cell number \(indexPath.row)."
return cell
}
Debug shows error in:
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath as IndexPath) as! customTableViewCell

I guess you use different identifier or the cell not configured in the storyboard as customTableViewCell

Related

Crash: Attempted to dequeue multiple cells for the same index path, which is not allowed

I have tableview datasource func to build a cell from a factory method function.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return OWTableViewOrganizer.instance.configureCell(at: indexPath)!
}
The factory method is here:
func configureCell(at indexPath: IndexPath) -> UITableViewCell? {
var cell = UITableViewCell()
switch indexPath.section {
case thisWorkoutSections.barbel.sectionNumber():
cell = barebellCell(indexPath: indexPath)
break
case thisWorkoutSections.lastWorkout.sectionNumber():
cell = lastWorkoutCell(indexPath: indexPath)
break
case thisWorkoutSections.personalRecord.sectionNumber():
cell = personalRecordCell(indexPath: indexPath)
break
case thisWorkoutSections.notes.sectionNumber():
break
default:
break
}
return cell
}
I have this code to build the cell:
func lastWorkoutCell(indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier(), for: indexPath) as! WorkoutSetTableViewCell
if OWTableViewOrganizer.instance.lastWorkoutExerciseSets.count > 0 {
if indexPath.row < OWTableViewOrganizer.instance.lastWorkoutExerciseSets.count {
let logExerciseSet = OWTableViewOrganizer.instance.lastWorkoutExerciseSets[indexPath.row]
let setNumber = indexPath.row + 1
if let weight = logExerciseSet.weight?.doubleValue, let reps = logExerciseSet.reps?.intValue {
cell.setupCellWithData(setNumber: setNumber, weight: weight, reps: reps)
}
} else {
cell.setupCellWithData(setNumber: -1, weight: 0, reps: 0)
}
} else {
cell.setupCellWithData(setNumber: -1, weight: 0, reps: 0)
}
return cell
}
But time to time this line crashes for me:
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier(), for: indexPath) as! WorkoutSetTableViewCell
With error:
Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path)
I know code style and design is not ideal here, please skip this if you have comments.
I don't know where to look, I tried simply remove indexPath, but it looks does not help or bring even more issues:
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier()) as! WorkoutSetTableViewCell
I have one controller which presents another one at the top of it (like in Apple music) and I can swipe down to show bottom controller and swipe up to bring back top controller. I noticed in log that I have some presentation alert, not sure if this something I need to deal with to resolve the issue above but JFY info.
It seems like there are two table view trigger
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
and you only dequeue cell from 1 table view at
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier(), for: indexPath) as! WorkoutSetTableViewCell
You should try to pass tableView from
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
to
lastWorkoutCell(indexPath: IndexPath)
(which will become lastWorkoutCell(indexPath: IndexPath, tableView: UITableView)) and dequeue cell from tableView

Add target to cell but having local variables create issues swift

I have a custom cell class. I have a button in a cell to which I have to add a target. But to do so, I have to implement a function for the action with the #objc keyword which I can't do as a nested function. Yes I have to make nested functions because of local variables such as indexPath and cell. Thanks
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCellSub", for: indexPath) as! newInvoicePopup_ItemTableViewCell
cell.textLabel!.text = (filteredItems[indexPath.row] as! ItemData).name
cell.detailTextLabel!.text = (filteredItems[indexPath.row] as! ItemData).tax
cell.priceLabel!.text = (filteredItems[indexPath.row] as! ItemData).price
cell.addButton.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)
return cell
}

Editor placeholder in source file error with simple UITableView

I am totally new to Swift and also have no background experience in development. I am trying to a simply table view in Swift but keep getting the error 'Editor placeholder in source file.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: <#T##IndexPath#>) as! CustomCell
cell.photo.image = images[indexPath.row]
cell.Name.text = names[indexPath.row]
cell.Job.text = jobs[indexPath.row]
return cell
}
This is where your problem is:
for: <#T##IndexPath#>)
just tab to that variable and type in:
indexPath

Use of undeclared type 'ContactsTableViewCell'

I am working on to fetch phone contacts. But I got an error like use of undeclared type ContactsTableViewCell. I tried UITableViewCell but it didn't work. What should I do ?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! ContactsTableViewCell
let data = dataArray![indexPath.row] as! Data;
cell.lblName.text = data.name
cell.imgContact.image = data.image
return cell
}
First you should make a class named ContactsTableViewCell which inherit UITableViewCell or if you already created it be sure you added this class to your targets.

Using UITableView and IndexPath with Swift 3 shows an error

I have this code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: IndexPath) as? Cell
But IndexPath is showing an error:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: IndexPath) as? Cell
In this line you're passing IndexPath Class instead of the indexPath parameter from the function. replace it with:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? Cell