I am using tableview with custom cell in swift 3.0. I take a textview and give outlet of class of tabelview cell and accessing in tableview delegate method but its showing me error like unexpectedly found nil while unwrapping an Optional value
here is my code. Record is array
Chek this Screenshot. FYI I have taken A new custom class and Give Outlet to it but same problem ocuured
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return record.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
if let str = record.object(at: indexPath.row) as? String {
print(cell.txtview)
cell.txtview.text = str
}
return cell
}
Its perfectly working in Static cell but in custom cell it showing me error.
cell.textview is not taken from custom cell
Please help me with it
Please try with following line not sure about that:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
if let title = record.object(at: indexPath.row) as? String {
cell.textview.text = title
} else {
cell.textview.text = ""
}
return cell
}
Related
I'm trying to use tableViews for the first time but I am running into an issue. When I run my program containing the code below, clicking on a cell is not registered. In addition, my custom cells (image below as well) do not display uniquely either. My code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Success!")
}
Declaring information:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//print(numApplications)
if numApplications != 0 {
}
//print("2nd?")
//print(numApplications)
if numApplications < 7 {
return 7
}
else {
return numApplications
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.white
tableView.rowHeight = 85
cell.textLabel?.text = "\(indexPath.row)"
//print(indexPath.row)
//print(applications.count)
if indexPath.row < applications.count {
cell.textLabel?.text = applications[indexPath.row].msg
}
return cell
}
Replace this
let cell = UITableViewCell()
with
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CellName
Where CellName is your class that you set for the prototype cell in IB with identifier cell
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.
I'm trying to get the text on a table view cell and then get the index of it in an array.
This is the code I'm using:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
let indexOfCellString = event.index(of: cell.textLabel!.text)
}
But I get the following errors:
value of optional type string? not unwrapped
invalid character in source file
What is going on here? What else do I need to unwrap?
There is no need to cast dequeueReusableCell to UITableViewCell since that is its return type.
cell.textLabel is optional. And then the text property of UILabel is optional.
Properly deal with optionals:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if let text = cell.textLabel?.text {
let indexOfCellString = event.index(text)
// do stuff with indexOfCellString
}
return cell
}
I'm trying to find a UITableViewCell text in an array. The problem is no matter what cell I'm clicking on, array.index(of:) always returns 1.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if let text = cell.textLabel?.text {
let indexOfCellString = event.index(of: text)
print (indexOfCellString!)
}
}
//here is my attempt to set the cell text:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if eventIndex.indices.contains(indexPath.row) == true {
cell.textLabel?.text = event [indexPath.row]
}
return cell
}
event is an array that i'm trying to search in and find cell's text.
and cell's text is always in an index of event. because my tableview reads the information from event array. therefore it shouldn't always return 1.
What am I doing wrong?
Never use dequeueReusableCell outside of cellForRowAt. Simply access your data from your data model.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if eventIndex.indices.contains(indexPath.row) == true {
let text = event[indexPath.row]
if let indexOfCellString = event.index(of: text) {
print("Found index: \(indexOfCellString)")
} else {
print("text not found")
}
}
}
When i made the migration swift code i have this error "Type 'Any' has no subscript members" and my code is
var myArray: NSMutableArray = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell()
if let name = self.myArray[(indexPath as NSIndexPath).row]["FirstName"] as? String{
cell.textLabel?.text = ("\(name)")
}
}
I tried many things but i do not have a answer for this problem.
First of all: Do not use NSMutableArray in Swift!
The error occurs because the compiler needs to know if the object is subscriptable by key. Using a native Swift type solves the problem.
var myArray = [[String:Any]]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell() // will not work
if let name = self.myArray[indexPath.row]["FirstName"] as? String { // optional binding actually not needed
cell.textLabel?.text = name // please no string interpolation
}
return cell // mandatory!
}
Note: Consider that UITableViewCell() will not work. The recommended way are reusable cells
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
You should use
Generics
dequeueReusableCell
IndexPath instead of NSIndexPath
so here's the code
import UIKit
class Controller: UITableViewController {
var persons = [[String:String]]()
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellID") ?? UITableViewCell(style: .default, reuseIdentifier: "MyCellID")
cell.textLabel?.text = persons[indexPath.row]["FirstName"]
return cell
}
}