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.
Related
I'm trying to create a custom cell view with some labels, I have created a subclass of UITableViewCell and connected the labels to it. Inside cellForRowAt method I dequeued the cell and cast it as YourCell subclass to access the labels and set text.
Setting class of cell to YourCell
However when running the app, nothing is showing up.
class YourCell: UITableViewCell {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let array1 = ["Name1", "Name2","Name3","Name3"]
let array2 = ["1","2","3","4"]
let array3 = ["18","17","11","9"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! YourCell
cell.label1.text = array1[indexPath.row]
cell.label2.text = array2[indexPath.row]
cell.label3.text = array3[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array2.count
}
}
You have conformed the UITableViewDelegate protocol correctly, the only thing you are missing is to set up your datasource by including this line in your viewDidLoad.
tableview.dataSource = self
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
}
Sorry for the Chinese characters, but the UILabel text alignment displayed as right side NO MATTER which option (left, center and etc.) it is set. Please help.
// This is the function that populates the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! WordLibraryTableViewCell
let key = Int(dataSource[indexPath.section][indexPath.row])
let value = levelDict[key!]! as String
cell.wordLibLabel.text = value
return cell
}
// This is the cell class
class WordLibraryTableViewCell: UITableViewCell {
#IBOutlet weak var bookimageView: UIImageView!
#IBOutlet weak var wordLibLabel: UILabel!
}
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