I am trying to create a custom cell for my tableView. I believe I followed all the correct steps but when I try to write code to access my custom properties from cellForRowAtIndexPath, I get the aforementioned error. It is as if it the custom properties cannot be seen.
Custom cell:
class MapViewTableViewCell: UITableViewCell {
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var distanceLabel: UILabel!
#IBOutlet weak var accessibleImage: UIImageView!
TableView:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as MapViewTableViewCell
cell.nameLabel.text = items[indexPath.row]
I verified that the custom cell objects in the storyboard are connected to the MapViewTableViewCell outlets. I also verified that the custom cell in the storyboard has its class set as MapViewTableViewCell.
I am using XCode 6.1.1
I was wondering if anyone could shed some insight as to what might be going wrong?
Thanks in advance.
With this line
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as MapViewTableViewCell
you are forcing the cell to be of type UITableViewCell which indeed does not have a member nameLabel. Change this line to
var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as MapViewTableViewCell
Related
I have a TableViewController that uses a custom prototype cell. The outlets for the cell contents are defined in a swift file named BYMyCell.swift.
class BYMyCell: UITableViewCell {
#IBOutlet weak var yearLabel: UILabel!
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet weak var startingDateLabel: UILabel!
#IBOutlet weak var commentsLabel: UILabel!
#IBOutlet weak var openStateImage: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
In my BusinessYearViewController which is a TableViewController view I define the cell for the CellForRowAt func as the following:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! BYMyCell
This casting "as! BYMyCell" works fine. I am able to put data in the outlets that are labels and an image into my custom cell.
Now I would like to make the cell or row swipeable. I have loaded the SwipeCellKit cocoaPod which I have used before when the cellForRowAt did not use the downcasting for the custom cell. The documentation suggest that I need to downcast the "let cell ..." definition to downcast to "as! SwipeCellKit" such as:
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! SwipeTableViewCell
I'm not sure if this will disable my outlets from the BYMyCell.swift file on behalf of my ViewController. Can someone suggest how I would handle both a custom cell and making it a cocoapod swipeable cell at the same time.
My reason is to put Edit, Flag, and Delete functions in the swipeable cell UI.
Another Swift beginner here. I simply want a Stepper in each of my TableView cells that increments a label in the same cell.
I have found a couple of questions on this topic, but they include other elements and I haven't been able to extract the basic concept.
Swift Stepper Action that changes UITextField and UILabel within same cell
Stepper on tableview cell (swift)
So far I have connected IBOutlets for my Label and Stepper, as well as an IBAction for my Stepper in my cell class.
class BuyStatsCell: UITableViewCell{
//these are working fine
#IBOutlet weak var category: UILabel!
#IBOutlet weak var average: UILabel!
#IBOutlet weak var price: UILabel!
//Outlet for Label and Stepper - How do I make these work?
#IBOutlet weak var purchaseAmount: UILabel!
#IBOutlet weak var addSubtract: UIStepper!
//Action for Stepper - And this?
#IBAction func stepperAction(_ sender: UIStepper) {
self.purchaseAmount.text = Int(sender.value).description
}
}
And I understand the concept of reusing the cell in the cellForRowAt indexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BuyStatsTabCell", for: indexPath) as! BuyStatsCell
cell.isUserInteractionEnabled = false
//these are working
cell.category.text = categories[indexPath.row]
cell.price.text = String(prices[indexPath.row])
cell.average.text = String(averages[indexPath.row])
//but is there something I need to add here to keep the correct Stepper and Label for each class?
return cell
}
One of the already asked questions includes a protocol and another function in the ViewController like this
protocol ReviewCellDelegate{
func stepperButton(sender: ReviewTableViewCell)
}
func stepperButton(sender: ReviewTableViewCell) {
if let indexPath = tableView.indexPathForCell(sender){
print(indexPath)
}
}
I don't know if this is the approach I should be trying to take. I am looking for the simplest solution, but I am having trouble putting the pieces together.
Any help is appreciated.
Thanks.
Easiest solution (simplyfied):
Create a model BuyStat with a property purchaseAmount (it's crucial to be a class).
You are strongly discouraged from using multiple arrays as data source
class BuyStat {
var purchaseAmount = 0.0
init(purchaseAmount : Double) {
self.purchaseAmount = purchaseAmount
}
}
In the view controller create a data source array
var stats = [BuyStat]()
In viewDidLoad create a few instances and reload the table view
stats = [BuyStat(purchaseAmount: 12.0), BuyStat(purchaseAmount: 20.0)]
tableView.reloadData()
In the custom cell create a property buyStat to hold the current data source item with an observer to update stepper and label when buyStat is set
class BuyStatsCell: UITableViewCell {
#IBOutlet weak var purchaseAmount: UILabel!
#IBOutlet weak var addSubtract: UIStepper!
var buyStat : BuyStat! {
didSet {
addSubtract.value = buyStat.purchaseAmount
purchaseAmount.text = String(buyStat.purchaseAmount)
}
}
#IBAction func stepperAction(_ sender: UIStepper) {
buyStat.purchaseAmount = sender.value
self.purchaseAmount.text = String(sender.value)
}
}
In cellForRowAtIndexPath get the data source item and pass it to the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BuyStatsTabCell", for: indexPath) as! BuyStatsCell
cell.buyStat = stats[indexPath.row]
return cell
}
The magic is: When you are tapping the stepper the label as well as the data source array will be updated. So even after scrolling the cell will get always the actual data.
With this way you don't need protocols or callback closures. It's only important that the model is a class to have reference type semantics.
NOTE: MY Cell class is just normal..All changes are in viewcontroller class
class cell: UITableViewCell {
#IBOutlet weak var ibAddButton: UIButton!
#IBOutlet weak var ibStepper: UIStepper!
#IBOutlet weak var ibCount: UILabel!
#IBOutlet weak var ibLbl: UILabel!
}
1.define empty int array [Int]()
var countArray = [Int]()
2.append countArray with all zeros with the number of data u want to populate in tableview
for arr in self.responseArray{
self.countArray.append(0)
}
3.in cell for row at
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! cell
let dict = responseArray[indexPath.row] as? NSDictionary ?? NSDictionary()
cell.ibLbl.text = dict["name"] as? String ?? String()
if countArray[indexPath.row] == 0{
cell.ibAddButton.tag = indexPath.row
cell.ibStepper.isHidden = true
cell.ibAddButton.isHidden = false
cell.ibCount.isHidden = true
cell.ibAddButton.addTarget(self, action: #selector(addPressed(sender:)), for: .touchUpInside)
}else{
cell.ibAddButton.isHidden = true
cell.ibStepper.isHidden = false
cell.ibStepper.tag = indexPath.row
cell.ibCount.isHidden = false
cell.ibCount.text = "\(countArray[indexPath.row])"
cell.ibStepper.addTarget(self, action: #selector(stepperValueChanged(sender:)), for: .valueChanged)}
return cell
}
4.objc functions
#objc func stepperValueChanged(sender : UIStepper){
if sender.stepValue != 0{
countArray[sender.tag] = Int(sender.value)
}
ibTableView.reloadData()
}
#objc func addPressed(sender : UIButton){
countArray[sender.tag] = 1//countArray[sender.tag] + 1
ibTableView.reloadData()
}
I have a generic cell ItemCell that can display any kind of item. The descendant of my Item class is my Armor class. I have a function inArmor that overrides a function in Itemthat returns an ItemCell. However no matter what label, when I try to change the text value of one I get the error below. I can't even set a hard coded string. I also checked the ItemCell class and it's .xib file and everything is linked. Is there something I'm missing? Let me know what code you would need to see as I have no idea where this issue is coming from.
fatal error: unexpectedly found nil while unwrapping an Optional value
CharacterTableView.swift
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UINib(nibName: "ItemCell", bundle: nil), forCellReuseIdentifier: "ItemCell")
switch sections[indexPath.section] {
case "Equipped":
return Character.inventory.equippedGear[indexPath.row].cell
case "Armor":
return Character.inventory.unequippedArmor[indexPath.row].cell
case "Weapons":
return Character.inventory.unequippedWeapons[indexPath.row].cell
case "Healing":
return Character.inventory.healingItems[indexPath.row].cell
default:
return ItemCell()
}
}
Armor.swift
override var cell: ItemCell {
let cell = ItemCell()
cell.name.text = "Name"
}
ItemCell.swift
class ItemCell: UITableViewCell {
//MARK: - IBOutlets
#IBOutlet weak var imageItem: UIImageView!
#IBOutlet weak var name: UILabel!
#IBOutlet weak var imageStat1: UIImageView!
#IBOutlet weak var labelStat1: UILabel!
#IBOutlet weak var imageStat2: UIImageView!
#IBOutlet weak var labelStat2: UILabel!
}
I see several potential problems in your code that can cause the crash.
You register your cell in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell, but you don't dequeue a cell. This looks to me as that you are returning a cell that has no relation to the tableview that is requesting the cell.
You have a UITableViewCell with many outlets, but you don't set these outlets anywhere. This means that all the outlets except the name will be nil and be explicitly unwrapped (because of the !) when displayed in the table view.
You switch on indexPath. section and assumes there the sections are Strings that you have defined. I can't check whether this is the case and if the datasource call really comes with the stings as section. Have your checked this while debugging?
As a default you bluntly return an empty ItemCell. By empty I mean that none of the outlets are set and therefor empty. The labels are explicitly unwrapped (because of the !) and will make your app crash.
I hope this helps you solve the problem.
To create cells for a table, you should be calling something like…
tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
Also, you might find it better to use an enum than an array of Strings for your sections:
enum Sections: Int {
equipped, armour, weapons, healing
}
And your cell should be registered just once. So…
enum Sections: Int {
equipped, armour, weapons, healing
}
override func viewDidload() {
super.viewDidLoad()
tableView.register(UINib(nibName: "ItemCell", bundle: nil), forCellReuseIdentifier: "ItemCell")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
guard let section = Section(rawValue: indexPath.section) else {
return cell
}
switch section {
case .equipped:
cell.item = Character.inventory.equippedGear[indexPath.row]
case .armour:
// etc
case .weapons:
// etc
case .healing:
// etc
}
return cell
}
How to solve the table view data overlapped issue?
I have searched some Object C version answer, but it seems not working for Swift. I tried to get cell==nil, but it gives me an error
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell = myTable.cellForRowAtIndexPath(indexPath) as! PatientTableViewCell
if (cell){
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
if(cell != nil){
var subviews = cell.contentView.subviews
subviews.removeAll()
}
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}
After two-days struggling, I figure it out finally. The key to solve it is to find which cell is been resued. What I am doing now is give a var identifier to cell.
class PatientTableViewCell: UITableViewCell {
#IBOutlet weak var followupBtn: UIButton!
#IBOutlet weak var viewBtn: UIButton!
#IBOutlet weak var titleType: UILabel!
#IBOutlet weak var titleID: UILabel!
#IBOutlet weak var patientTypelbl: UILabel!
#IBOutlet weak var patientIDlbl: UILabel!
**var identifier = false**
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
var cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
if(cell.identifier == true){
cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
//config cell
cell.identifier = true //important
I hope it can help someone else. :)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell: PatientTableViewCell! = tableView.dequeueReusableCellWithIdentifier("patientCell") as? PatientTableViewCell
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}
I created a TableView directly in the storyboard; this tableView contains 8 static cells (style basic) in 4 section; now, how could I edit via code these cells? for example for to change textLabel, backgroundColor, separator ecc
I tried to set an identifier to each cel but didn't work...
For static cells created in the storyboard, you can simply set the IBOutlet for the elements you want to edit by ctrl-dragging from the storyboard to the corresponding view controller, to end up with something like this:
class MyViewController: UIViewController {
#IBOutlet weak var cell1: UITableViewCell!
#IBOutlet weak var cell2: UITableViewCell!
}
Then you can access the built-in elements in the Basic view with cell1.textLabel etc.
Check out the documentation about setting IBOutlets.
To change the background color, you can do it in the storyboard UI directly or access the backgroundColor property. You might want to read the UITableViewCell Class Reference.
Thanks p4sh4 for #IBOutlet suggestion.
But instead of multiple Outlets, you should try an "Outlet Collection".
#IBOutlet var tableLabelCollection: [UILabel]!
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
print("accessory Button Tapped: \(indexPath.row) => \(tableLabelCollection[indexPath.row].text)")
}