I have the following Swift code, which is mostly auto generated once a tableview is added:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "test")
return cell.textLabel.text = "test"
}
I get the following compile time error:
Cannot assign a value of type 'String' to a value of type 'String?'
I have tried ! (unwrapping) in the cell.textLabel.text syntax to no effect. Any ideas what I'm doing wrong?
You're supposed to be returning the cell, not text from cellForRowAtIndexPath. You also should be using dequeueReusableCellWithIdentifier to get your cell.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("test") as UITableViewCell!
cell.textLabel?.text = "test"
return cell
}
The textLabel as a part of the UITableViewCell is optional and therefore you need to change your code to this:
cell.textLabel?.text = "test"
return cell
To add on to this you should not be getting your cell using that method, you should use:
let cell = tableView.dequeueReusableCellWithIdentifier("test", forIndexPath: indexPath) as UITableViewCell
So in the end your code should look like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("test", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "test"
return cell
}
Related
So I have this function.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
changeCellProperty(selectedIndexPath: indexPath)
return cell;
}
func changeCellProperty(selectedIndexPath: IndexPath){
print("indexpath = \(selectedIndexPath)") . // printing [0,0] and all values
let cell = self.tableView.cellForRow(at: selectedIndexPath) as! customCell
// got nil while unwrapping error in above statement.
cell.label.text = ""
// and change other properties of cell.
}
I am not able to understand the error.
When I am getting the indexpath, then why I am not able to point a particular cell and change properties accordingly.
You cannot access a cell that has not yet been added to the tableView. That is what you are trying to do here in changeCellProperty method. So, if your dequeue works, then all you would need to do is pass the dequeued cell to that method.
func changeCellProperty(cell: customCell){
cell.label.text = ""
// and change other properties of cell.
}
Your cellForRowAt method would look like this.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
changeCellProperty(cell: cell)
return cell
}
Note: class names should be UpperCamelCase. So your customCell should be named CustomCell.
An error keeps occurring even changing the new swift3.0 syntax like removing the NS prefixes and other. The error says
cannot call value of non-function type UITableView
I'm glad if I could have a hint for solving this problem.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell
}
You need to return cell because cellforRowAtIndexPath is non void function :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cartcel", for: indexPath) as! cartTableViewCell // My custom tableview cell class
cell.productName.text = Addtocartdata[indexPath.row].cartproName
cell.productQty.text = Addtocartdata[indexPath.row].cartproPrice
cell.productAmount.text = Addtocartdata[indexPath.row].cartproPrice
return cell;
}
let cellIdentifier = "ChampionThumbnailCell"
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? ChampionThumbnailTableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
}
cell!.championNameLabel.text = championNames[indexPath.row]
cell!.championThumbnailImageView.image = UIImage(named: championThumbnail[indexPath.row])
return cell!
}
I want to use the above code to reuse UITableViewCell, but I got this error when build it.
It looks like thie: Cannot assign value of type 'UITableViewCell' to type 'ChampionThumbnailTableViewCell?'
Is there any solution could fix it?
(By the way, I'm not very good at English...)
Change your method to this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ChampionThumbnailCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ChampionThumbnailTableViewCell
cell.championNameLabel.text = championNames[indexPath.row]
cell.championThumbnailImageView.image = UIImage(named: championThumbnail[indexPath.row])
return cell
}
Notice in third line I am using as! instead of as? showing compiler that you aren't confused about your cell being nil.
But if it turns out that your cell is nil, it should cause runtime error.
I am trying to make a TO-DO list app in Xcode by using Swift, and I encounter an error writing one of the function methods on the "if let path = indexPath {" line which says "Bound value in a conditional binding must be of Optional type".
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
if let path = indexPath {
let currentString = dataSource[path.section][path.row]
cell.textLabel?.text = currentString
}
return cell
}
Because indexpath is not optional,you do not need to use conditional binding
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let currentString = dataSource[indexPath.section][indexPath.row]
cell.textLabel?.text = currentString
return cell
}
why u want use two constant?
fix ure code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = dataSource[indexPath.section][indexPath.row]
}
return cell
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = dataSource[indexPath.section][indexPath.row]
return cell
}
Use this code because I think my previous idea was not good.
You don't need conditional binding here as suggested by Leo.
The below code is supposed to take the name field from the PFObject being passed into cellForRowAtIndexPath and set the textLabel.text field to the name (ie the tableView row could potentially read "sneeze1"). I'm able to change the height of each row with the heightForRowAtIndexPath method and also see the correct number of calls to cellForRowAtIndexPath (3 rows loaded into a class on Parse's Data Browser in my specific case) with the right fields, but I can't get the textLabel.text to change.
Is there some other step that needs to be completed before a textLabel can have its text changed?
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 60
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
var cellIdentifier = "EventCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? PFTableViewCell
if !cell {
cell = PFTableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
}
var name = object["name"] as? String
cell!.textLabel.text = name
println("textLabel: \(cell!.textLabel.text)" + " name: \(name)")
return cell
}
You must use the original delegate signature, else your method won't get called. Instead of
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
use
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
The issue is that as? returns an optional type, so name is of type String?.
Try this instead if you really want to handle missing names:
if let name = object["name"] as? String {
cell!.textLabel.text = name
}
I was able to get the text to change when I set used the UITableViewCell cellForRowAtIndexPath. It looks like the problem is with Parse's PFTableViewCell potentially mixing with Swift. It's not allowing me to change the text on PFTableViewCell.
As a side note, the code works even when the below method exists in my PFQueryTableViewController side-by-side with the above PFTableViewCell cellForRowAtIndexPath. Somehow, the code is actually just running the UITableViewCell cellForRowAtIndexPath method instead.
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cellIdentifier = "EventCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? UITableViewCell
if !cell {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
}
var name = "HELLO"//object["name"] as? String
cell!.textLabel.text = name
println("textLabel: \(cell!.textLabel.text)" + " name: \(name)")
//cell.imageView.file = object.objectForKey(self.imageKey) as PFFile
return cell
}