Error: Variable with getter/setter cannot have an initial value - swift

How do I fix this error?
Variable with getter/setter cannot have an initial value
Here's my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableview.dequeueReusableCellWithIdentifier("cell") as UITableViewCell { //Error
cell.textLabel?.text = self.items[indexPath.row] //Error
return cell; //Error
}
}

I think you have an extra set of {} As it is, you're defining a block and assigning it to the cell variable:
var cell: UITableViewCell = tableview.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row] //Error
return cell; //Error
And, since dequeueReusableCell... returns a UITableViewCell already, all you really need is:
var cell = tableview.dequeueReusableCellWithIdentifier("cell")
My guess is you cut/copied code that started out looking like:
if let cell = tableview.dequeueReusableCellWithIdentifier("cell") as? MyCellClass {
// some setup code here
return cell
}

Related

Set the colour of all the labels in tableview

How would I set the colour of each label in table view section using swift?
I have done this but it does not work.
Thanks
for section in 0..<tableView.numberOfSections {
for row in 0..<tableView.numberOfRows(inSection: section) {
let indexPath = NSIndexPath(row: row, section: section)
var cell = tableView.cellForRow(at: indexPath as IndexPath)
if (cell as? UILabel) != nil {
cell?.textLabel?.textColor = .red
}
}
}
You should be set the cell's properties inside the cellForRowAt method, like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: <#T##String#>, for: indexPath)
cell.textLabel?.backgroundColor = .red
return cell
}
You are making a conflict with type.
In this line you are assigning the cell type to variable cell
var cell = tableView.cellForRow(at: indexPath as IndexPath)
And In this line of code you are asking if cell is a UILabel then make it text label colour to red.
This will always return false and code inside if will never execute.
if (cell as? UILabel) != nil {
cell?.textLabel?.textColor = .red
}
Remove the if statement and then execute the code.

Send different nibs in a function swift 3

I have a tableView who need to contain two different view, the name of the first one is CustomTableViewCell the second one is CustomDeliveryTableViewCell
I want my variable to take the two cell, I don't understand this error.
Here my function
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell: UITableViewCell
cell = Bundle.main.loadNibNamed("CustomTableViewCell", owner: self, options: nil)?.first as! UITableViewCell
if (!self.appReady)
{
return cell
}
let arrayOfCard = self.selectedCard(section: indexPath.section, row: indexPath.row)
let json:JSON = JSON(arrayOfCard)
if (json[0]["cards"][indexPath.row]["category"] == "delivery")
{
cell = Bundle.main.loadNibNamed("CustomDeliveryTableViewCell", owner: self, options: nil)?.first as! CustomTableViewCell
}
cell = fillCell(cell: cell, json: json, index: indexPath.row)
return cell
}
My fonction fillCell is prototype like that
func fillCell(cell: CustomTableViewCell, json:JSON, index:Int) ->
CustomTableViewCell
Edit
Here the code of actual fillCell function
func fillCell(cell: UITableViewCell, json:JSON, index:Int) -> UITableViewCell {
if (json[0]["cards"][index]["category"] == "train")
{
if let type = json[0]["cards"][index]["category"] as JSON?
{
cell.labelType.text = type.string
}
if let departureStation = json[0]["cards"][index]["train"]["departure"]["station"] as JSON?
{
cell.labelDepartureStation.text = departureStation.string
}
// Do some code
}
else if (json[0]["cards"][index]["category"] == "delivery")
{
//Do some code
return cell
}
else{
//Do some code
return cell
}
}
Your initial assignment creates a cell of type CustomDeliveryTableViewCell.
Within the if block you're trying to assign a CustomTableViewCell to the same variable. This will only work if CustomTableViewCell is a subclass of CustomDeliveryTableViewCell
When you call fillCell( it's expecting CustomTableViewCell, but cell is a CustomDeliveryTableViewCell
If you declare var cell: UITableViewCell then you can assign either type to it.

Swift 2 - UITableView with Multi sections

I created a table with three sections, each section should handle an Animal array
var firstAnimals = Animal
var secondAnimals = Animal and so on
I am having trouble in the cellForRowAtIndexPath function, I want it to insert the items of the arrays according to the right section. I tried this, but it doesn't work with the array. Please help!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! myCell
if indexPath.section == 0 {
cell.title.text = firstAnimals[indexPath.row]
} else if indexPath.section == 1 {
cell.title.text = secondAnimals[indexPath.row]
} else {
cell.title.text = thirdAnimals[indexPath.row]
}
return cell
}
The recommended way is to use a nested array
let animals = [[firstAnimal1, firstAnimal2, firstAnimal3],
[secondAnimal1, secondAnimal2, secondAnimal3],
[thirdAnimal1, thirdAnimal2, thirdAnimal3]]
Then cellForRowAtIndexPath could look like
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! myCell
let animalSection = animals[indexPath.section]
let animal = animalSection[indexPath.row]
cell.title.text = animal.name
return cell
}
Alternatively use an array with the section names and a dictionary with an animal array for each section name respectively. The section name array can also be used as the section header names.

Trouble passing tableview cell value to delete cell

I am trying to pass a value to update a tableview cell. Problem is I am not able to get the value. The tableview cell code is:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell;
// Gets staff name and puts it in the cell title
var sectionTitle:String = aventurasUnicas.objectAtIndex(indexPath.section) as String
var sectionStaff:Array = porAventura[sectionTitle]!
let staffNic:String = sectionStaff[indexPath.row] as String
cell.textLabel?.text = staffNic
// Gets the subtitle
var subtituloAventura:String = "\(sectionTitle).\(staffNic)" as String
var sectionStaff2:Array = subtituloTabla[subtituloAventura]!
let staffNic2:String = sectionStaff2[0] as String
cell.detailTextLabel?.text = staffNic2
cell.accessoryType = UITableViewCellAccessoryType.DetailButton
return cell
}
And the code I am stuck with is:
override func tableView(tableView: UITableView?, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath?) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// I AM STUCK HERE
}
}
I want to get the staffNic for the next part of the program. Thanks for your help
Why not go this way - the way you get it is the way you set it:
let sectionTitle:String = aventurasUnicas.objectAtIndex(indexPath.section) as String
let sectionStaff:Array = porAventura[sectionTitle]!
//the text of the deleted row based on your datasource
let staffNic = sectionStaff[indexPath.row] as String
Or get it as cell's text:
let cell = tableView.cellForRowAtIndexPath(indexPath)!
let staffNic = cell.textLabel!.text!

Swift: How to make a selected cell NSLog the corresponding value and key from a dictionary

I have a TableViewController that is populated by a dictionary. My goal is that when I click a cell from the tableView it will NSLog the cell's name from the dictionary as well as the corresponding value.
For example if I have a dictionary:
var profiles = ["Joe": 1, "Sam": 2, "Nancy": 3, "Fred": 4, "Lucy": 5]
When I click the Sam it would show up "Sam. 2" or something like that.
Any help would be great.
Here's a sample of my code (TableView) :
class ProfileTableViewController: UITableViewController {
var person = people ()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = indexPath.row
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
let myRowKey = person.typeList[row]
let myRowData = person.profiles[myRowKey]
cell.textLabel!.text = myRowKey
cell.textLabel?.text = String(myRowKey)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Here's where I'm at
}
Here's my swift file:
class people {
var profiles = ["Joe": 1, "Sam": 2, "Nancy": 3, "Fred": 4, "Lucy": 5]
var typeList:[String] { //computed property 7/7/14
get{
return Array(profiles.keys)
}
}
So, ideally, you want to use the index path that the method provides to grab whatever cell was selected.
Once you have that, you can extract the text from the cell and check with the dictionary.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Lots of optional chaining to make sure that nothing breaks
if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath) { // Get the cell
if let cellTextLabel: UILabel = cell.textLabel { // Get the cell's label
if let name: String = cellTextLabel.text { // Get the text from its label
println("\(name): \(profiles[name])") // Check with the dictionary and print out the corresponding value
}
}
}
}
I would get the text in the label and use it to search the dictionary as such:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Get the cell for that indexPath
var cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
// Get that cell's labelText
let myKey = cell.textLabel?.text
// Output the key and it's associated value from the dictionary
println("\(myKey): \(person.typeList[myKey])")
}