Use of undeclared type 'ContactsTableViewCell' - swift

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.

Related

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
}

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

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.

Create object that conform to protocol in Swift

I want to create cell that i know, is conformable to specific protocol.
However, when i try to do :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell<ModelBinding> = tableView.dequeueReusableCell(withIdentifier: "VacanciesCell", for: indexPath as IndexPath)
return cell
}
I got an error. How to fix it?
You have to subclass cell , which confirms the protocol that you want to use. Here i have created a sample protocol and CustomCell which confirming the protocol i have created.
1. Sample protocol
protocol MyProtocol {
func protocolMethod()
}
2. Custom subClassed cell
class CustomCell:UITableViewCell,MyProtocol {
//Implementation of Protol method
func protocolMethod() {
}
}
3. Use of that cell on tableview
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustomCell = tableView.dequeueReusableCell(withIdentifier: "VacanciesCell", for: indexPath as IndexPath) as! CustomCell
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