Using UITableView and IndexPath with Swift 3 shows an error - swift

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

Related

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
}

I have a problems with compiler my project UITableViewCell

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

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

swift 3 pass data from a tableView to another TableVIew?

I populated a tableView with an array of dictionaries Now How can I save the cell contents to Core Data and retrieve it in another tableView ?
I loaded an array of dictionaries to a tableView:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SourceTableViewCell
cell.titleLabel.text = sourceFeeds[indexPath.row]["title"]
cell.linkLabel.text = sourceFeeds[indexPath.row]["link"]
return cell
}
Now I want to save those link and title to core data in cell selection
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
let cell = self.tableView.cellForRow(at: indexPath) as! SourceTableViewCell
if self.tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.none {
self.tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
} else {
self.tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
}
I want to mention that "sourceFeeds" array has loaded from plist!
I found the solution of my problem finally!

How to add two custom cell into tableview in storyboard in Swift?

I have a tableView with two different custom cell. One cell have a switch and other a image, I have Two separated custom class for the cells, identifiers.. but I can't see this. i don't know if I need change the configuration in story board to dynamic or other thing.. Can I show two different custom cells
Thanks!
Check the criteria for showing each specific custom cell, then cast to that cell as needed:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
if (criteria for cell 1) {
let cell = tableView!.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as? Cell1
return (cell)
}
else {
let cell = tableView!.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as? Cell2
return (cell)
}
}
Swift 3
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath -> UITableViewCell {
if (criteria for cell 1) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2
return cell
}
}
override func tableView(tableView: UITableView?,cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
if (...) {
let cell : Mycell1 = tableView!.dequeueReusableCellWithIdentifier("Mycell1", forIndexPath: indexPath) as? Mycell1
return cell
}
else {
let cell : Mycell2 = tableView!.dequeueReusableCellWithIdentifier("Mycell2", forIndexPath: indexPath) as? Mycell2
return cell
}
}