hey so I'm trying to create a custom cell and I've properly linked everything in my story board but for some reason it doesn't display anything when I run. I Tried testing it out, it appears that the custom cell objects don't even get created.
class customCell : UITableViewCell{
#IBOutlet weak var EventImage: UIImageView!
#IBOutlet weak var type: UILabel!
#IBOutlet weak var rating: UIImageView!
#IBOutlet weak var date: UILabel!
#IBOutlet weak var name: UILabel!
}
class EventHistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var TableView: UITableView!
let k = ["hi","hey","yo","hello"]
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return k.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.TableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customCell
cell.name!.text = self.k[index.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
self.TableView.register(customCell.self, forCellReuseIdentifier: "cell")
TableView.delegate = self
TableView.dataSource = self
}
what am I doing wrong? the program in this case crashes when it reaches cell.name!.text = self.k[index.row]
Check this one
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: VC_FavouriteCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! VC_FavouriteCell
cell.name.text = k[index.row]
return cell
}
In place of cell.name!.text write cell.name.text
Don't register the cell
Related
I am developing a ToDo app in Swift, I am using Firebase to save data for my Swift App, but I can't delete it from collection, which called "tasks", only cells from Table View are deleting. I use Swift 5 and Xcode 12.5.1
Any help is welcome.
class ToDoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
#IBOutlet weak var statusCheckBox: UIImageView!
#IBOutlet weak var titleTextField: UITextField!
#IBOutlet weak var bodyTextField: UITextView!
#IBOutlet var blurView: UIVisualEffectView!
#IBOutlet var popupView: UIView!
#IBOutlet weak var tableView: UITableView!
let db = Firestore.firestore()
var tasks: [Task] = []
// Getting number of existing tasks
extension ToDoViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
// Creating tasks from reusable cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.FStore.reusableCell, for: indexPath)
cell.textLabel?.text = tasks[indexPath.row].title
cell.detailTextLabel?.text = tasks[indexPath.row].body
if tasks[indexPath.row].status {
cell.accessoryType = UITableViewCell.AccessoryType.checkmark
} else {
cell.accessoryType = UITableViewCell.AccessoryType.none
}
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
tasks.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
// here I need to delete data from Firebase
}
}
I have a TableView and for it I created a cell in xib. This cell has a label and a button. I would like to display the label and hide the button under certain conditions for the cell I need. How can i do this?
extension SourcesViewController: UITableViewDelegate, UITableViewDataSource {
...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: SourceCell.reuseId, for: indexPath) as! SourceCell
...
return cell
}
class SourceCell: UITableViewCell {
...
#IBOutlet var logInLabel: UILabel!
#IBOutlet var logOutButton: UIButton!
...
#IBAction func logInLogOutButton(_ sender: Any) {
//When I press this button I want to hide log out button and show label for current cell
}
}
My Custom Cell
import Foundation
import UIKit
class MyCustomCell : UITableViewCell {
#IBOutlet weak var myCustomLabel : UILabel!
#IBOutlet weak var myCustomButton : UIButton!
#IBAction func buttonClicked(_ sender : UIButton) {
sender.isHidden = true
}
}
TableView :
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! MyCustomCell
cell.myCustomLabel.text = "Row \(indexPath.row)"
// Configure the cell...
return cell
}
Here is what the output looks like :
For some reason, the delegate method is not being called. I am printing the delegate property in the console but it returns nil. I was looking for other answers here, but none of them were helpful for me.
protocol FilterSwitchDelegate {
func switchValueChanged(tagValue:Int)
}
class FilterTableViewCell: UITableViewCell {
#IBOutlet weak var filterLabel: UILabel!
#IBOutlet weak var filterSwitch: UISwitch!
var delegate:FilterSwitchDelegate?
#IBAction func filterSwitchValueChanged(_ sender: UISwitch) {
print(sender.tag)
delegate?.switchValueChanged(tagValue: sender.tag)
}
}
//In FilterViewController I am calling this delegate method
class FilterViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var dataSource = FilterTableViewCell()
var filterModel = [FilterVCDataModel]()
var ids = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
dataSource.delegate = self
}
}
extension FilterViewController: FilterSwitchDelegate {
func switchValueChanged(tagValue: Int) {
ids.remove(at: tagValue)
print(ids)
}
}
TableView dataSource and delegate methods
extension FilterViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filterModel.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FilterTableViewCell
cell.filterSwitch.tag = indexPath.row
cell.filterLabel.text = filterModel[indexPath.row].filterData["title"] as? String
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 66
}
}
in the cellForRow you have to dequeue your cell.
let cell = tableView.dequeueReusableCell(withIdentifier: "yourId") as? FilterTableViewCell
cell.delegate = self
return cell
and the job is done
i have create expandable table view in that tableview cell i have build xib file but i want to know that how i can twiddle for in loop of that xib file?
Custom Cell1
#IBOutlet weak var middleLabel: UILabel!
#IBOutlet weak var leftLabel: UILabel!
#IBOutlet weak var rightLabel: UILabel!
ViewController
let items = ["Item 1", "Item2", "Item3", "Item4"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomCellOne")
}
// MARK: - UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell
cell.middleLabel.text = items[indexPath.row]
cell.leftLabel.text = items[indexPath.row]
cell.rightLabel.text = items[indexPath.row]
return cell
}
}
Please try below code
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
let items = ["Item 1", "Item2", "Item3", "Item4"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomOneCell")
self.tableView.dataSource = self
self.tableView.delegate = self
}
//MARK:- UITableView DataSource & Delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOneCell") as! CustomOneCell
cell.middleLabel.text = items[indexPath.row]
cell.leftLabel.text = items[indexPath.row]
cell.rightLabel.text = items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44.0 //give height you want
}
}
I guess you have xib and custom UITableViewCell class, according to the information provided.
This is what you need.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("CellXib", owner: self, options: nil)?.first as! XibCustomCell
return cell
}
You can follow the following tut. easy and understandable
https://medium.com/#musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722
I have a UICollection view that has two custom cells. The UICollectionViewCell custom cells are built using NIB's. Inside one of the UICollectionViewCell NIB's, I have a UITableView. However, the UITableView itself has custom cells. So my question is:
How do I load a custom cell for the UITableView instead a UICollectionViewCell NIB?
import UIKit
class HeaderFooterCollectionViewCell: UICollectionViewCell {
//Tableview
#IBOutlet weak var tableView: UITableView!
//Buttons
#IBOutlet weak var resetButton: UIButton!
#IBOutlet weak var periodButton: UIButton!
#IBOutlet weak var undoButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// Register cell classes
tableView.register(UINib(nibName: "ShotInformationTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
}
}
extension HeaderFooterCollectionViewCell: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShotInformationTableViewCell
return cell
}
}
let cell = Bundle.main.loadNibNamed("XIB_File_Name", owner: self, options: nil)?.first as! XIB_Class_Name
In almost all cases , XIB_File_Name == XIB_Class_Name