Swift Newbie Argument labels '(_:, IndexPath:)' do not match any available overloads - swift

When I call the tableView function I get the error in the title. My question is why won't the function take the two arguments even though they are of the requested type? Again I'm a newbie so please forgive me if the answer is obvious.
class TableViewController: UITableViewController {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! UITableViewCell
let name = Array(shopItems.keys) [indexPath.row]
let cost = Array(shopItems.values) [indexPath.row]
cell.textLabel?.text = name + String(cost)
return cell
}
}
When I call the function like this:
"TableViewController.tableView(shopTableView, IndexPath: NSIndexPath)" I get the error: "Argument labels '(_:, IndexPath:)' do not match any available overloads"

Try use
let name = shopItems.keys[indexPath.row]
Instead of
let name = Array(shopItems.keys) [indexPath.row]
Better don't use force wrap, when it's not nesesery.
Try change
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! UITableViewCell
to
guard let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as? UITableViewCell else {
return UITableViewCell()
}
EDIT: As #Sh_Khan say replace
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
to
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

There is an easy and quick way to figure out yourself the proper overload
Select the entire method and press ⌘/ to comment out the code.
On the top level of the class type cellForRow. The first item in the code completion list is the proper method.
Press Return to insert the correct method.
Comment in the wrong method by selecting it again and pressing ⌘/.
Copy and paste the body of the wrong method into the correct one.
Delete the rest of the wrong method.

Related

Set String Identifier to Cell in Swift

I have a TableView that receives an array of string identifiers, what i need to do is to set an identifier for every cell that i have and associate it to a variable called "cellPressedId", but when i do that, i just obtain the last value of the array
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let selectedCell = tableView.cellForRow(at: indexPath) {
if let selectedCell = tableView.cellForRow(at: indexPath) {
var cellPressed = self.cellPressedId
}
}
What I need to do is to set this identifier to every cell and when i pressed this cell obtain the identifier
when a cell is selected you have access to the indexPath of the selected cell via this function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
and I'm sure you have access to your array of data (array of strings you mentioned that) you can access selected elements like this :
let selectedItem = myArray[indexPath.row]

Configure TableView Cells With A Set

I'm not sure how to configure my custom cells to use a set in a tableView. I want to be able to display my cells but I get the syntax error Cannot call value of non-function type 'Set' in my cellForRowAt with my itemFav, how would I configure my current index to use a set?
var favSet = Set<CurrentPlayers>()
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "favcell", for: indexPath) as! FavCell
let itemFav = favSet(indexPath.row) //How do I configure this to a set
cell.update(with: itemFav)
return cell
}
As I understand, you need access by index. Try this:
favSet[favSet.index(favSet.startIndex, offsetBy: indexPath.row)]

Typecasting with variable

I have an enum that will indicate the identifier and class to use for a tableViewCell.
enum Settings: String {
case Delete = "deleteCell"
var cellType: UITableViewCell.Type {
switch self {
case .Delete: return DeleteCell.self
}
}
}
In my tableView rowForIndexPath I am able to get the cell with the reuseIdendifyer, but it won't recognize the setting object to get the class. The error here is Use of undeclared type 'setting', specifically on the end of the line.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let setting = Settings.enabledSettings[indexPath.row]
return tableView.dequeueReusableCell(withIdentifier: setting.rawValue, for: indexPath) as! setting.cellType
}
It's not bad now with just the one object, but this will be growing soon and it's mostly the same thing, just changing the class name. Is it possible to pull the class type from another source like this so I can keep the rowForIndexPath function short and sweet, or do I need to do something like this?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let setting = Settings.enabledSettings[indexPath.row]
var cell = tableView.dequeueReusableCell(withIdentifier: setting.rawValue, for: indexPath)
if setting == .Delete { cell = cell as! DeleteCell }
return cell
}

Type 'Int' has no subscript members?

I am coding my first application in Swift. Right off the bat I am sorry if this is a duplicate as I have looked at others errors and wasn't really sure how to insert the help they got into my code.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textView.text.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myText = Int(entertextTextView.text!)
cell.textLabel?.text = "\(myText?[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
return cell
}
I get a "Type 'Int' has no subscript members" error. I am trying to add a new cell into a textview for each character in a textview.
If you are adding a character to every cell, you shouldn't need to convert it to a Int. Only when it is a string will you be able to access a character using indices.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myText = entertextTextView.text!
cell.textLabel?.text = "\(myText[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
return cell
}
Also, it doesn't matter if your content is a number or not, which is why i'm assuming you changed it to an Int. Unless you are performing arithmetic operations or similar operations on the number, you can just let the content be a String.

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.