I have a custom cell that I'm designing.
I want to pass a string to that custom cell. I made a cocoa touch class called AlarmCell (subclass of UITableCell), and then called it with this code (in my UIViewController class):
let myCell = AlarmCell()
And then called the UILabel from within AlarmCell.
myCell.timeLabel?.text = newAlarm
newAlarm has the string "08:38 PM"
How come myCell.timelabel is coming out as nil? What am I doing wrong?
Edit:
AlarmCell Class:
class AlarmCell: UITableViewCell {
#IBOutlet var timeLabel: UILabel!
}
Alarm(UIViewController Class):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = tableView.dequeueReusableCellWithIdentifier("alarmCell", forIndexPath: indexPath) as UITableViewCell
I had:
cell.textLabel?.text = alarms[indexPath.row]
But I need to pass a string to a custom labl inside the cell
Do yourself a favor and create a nib file associated with your AlarmCell.swift class. After customizing the cell inside the nib file, declare the following inside your viewDidLoad function.
var nib = UINib(nibName: "AlarmCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
Then, mention it in your cellForRowAtIndexPath function as so:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:AlarmCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as AlarmCell
cell.timeLabel.text = alarms[indexPath.row]
return cell
}
Try changing cell.textLabel?.text = alarms[indexPath.row] to
Cell.timeLabel?.text = NSString(format: "%#", alarms[indexPath.row])
And you should change the let keyword used for declaring the Cell object inside func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell method as follows:
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("alarmCell") as UITableViewCell alarmCell
Define Cell as
var cell: AlarmCell = self.tableView.dequeueReusableCellWithIdentifier("alarmCell") as UITableViewCell AlarmCell
then setTextLabel(or your Custom Label ) text as
cell.textLabel?.text = alarms[indexPath.row] as NSString
comment below if any query as this working perfectly for me
Related
I have a table using custom cells, within a custom cell i have a picker and a textfield.
I have a custom class for the cell that has outlets for these elements.
In the parent VC of the table I want to be able to reference the contensts of the text field and the value of the picker.
How do i gain access to these values when they are in a custom cell rather than just on the main viewcontroller view?
Code for cell:
class NewExerciseTableViewCell: UITableViewCell {
static let reuseIdentifier = "Cell"
#IBOutlet weak var setNumber: UILabel!
#IBOutlet weak var repsPicker: UIPickerView!
#IBOutlet weak var userExerciseWeight: UITextField!
}
I tried to access it by creating let setCell = NewExerciseTableViewCell() and then trying to access its contents components via its properties, but thats not the way to do it!
Appreciate any assistance here on how I can pull out the values in this cell!
edit: here is my callForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NewExerciseTableViewCell else {
fatalError("Unexpected Index Path")
}
cell.backgroundColor = UIColor.customBackgroundGraphite()
cell.textLabel?.textColor = UIColor.white
return cell
}
You need to create an object of UITableViewCell with the type of your custom type. Let say you are making a table view then you can add the cells in cellForRowAt. See below code for reference
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! NewExerciseTableViewCell
cell.setNumber.text = // Some Value
}
In the above method the cell is now of type NewExerciseTableViewCell and you can now access any public property of that class.
If you want to get the values of particular cell then you will need to get the cell first. Check the below code for reference
let cell = yourTableView.cellForRowAtIndexPath(indexPath) as! NewExerciseTableViewCell
print(cell.setNumber.text)
This question contains multiple parts, and I'll try to address them one by one.
1- First of all if you want to set the values, properties, etc. you have to do that in tableView(_:cellForRowAt:) method.
(Set the cellIdentifer in your storyboard.)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIndetifier", for: indexPath) as! NewExerciseTableViewCell
cell.setNumber.text = "1"
...
return cell
}
2- You cannot access cell's subview just by creating a new one. That doesn't make sense. Use let cell = tableView.cellForRow(at: ...) and then you have a valid cell.setNumber
P.S. btw the setNumber is not a good practice for naming a label. Use set only for setter methods.
I have a custom class for a table cell that is connected to a switch within the table cell (with an action) and I want to be able to to communicate to the TableViewController that the action happened as well as the path of the cell. The way that initially came to mind was if I could use some function in UITableViewCell to get the TableViewController of the table the cell is part of, as my custom class is (rather obviously) a subclass of UITableViewCell. Please tell me if I'm missing something.
To get a reference to the containing view controller, I add a weak property on the cell subclass.
#property (nonatomic, weak) UITableViewController *viewController;
You can assign this value in cellForRowAtIndexPath:
For accessing each cell in TableView
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as YourTableViewCell
cell.mainTextLabel.text = self.venueService.mainCategoriesArray()[indexPath.row]
return cell
}
For getting selected cell in TableView
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell;
print(currentCell)
}
Reaching from TableViewCell to TableView
self.superview //// self is TableViewCell and its superview represents TableViewController
hope that helps
You should make the ViewController the target of the switch action.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchCell", forIndexPath: indexPath) as! SwitchTableViewCell
cell.onSwitch.addTarget(self, action: Selector("cellSwitchDidChange:"), forControlEvents: .ValueChanged)
cell.label.text = "This is a Switch"
return cell
}
func cellSwitchDidChange(sender: UISwitch) {
let origin = sender.convertPoint(CGPointZero, toView: tableView)
let indexPath = tableView.indexPathForRowAtPoint(origin)!
// do something
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel!.text = self.funlists[indexPath.row]
return cell
}
cannot invoke 'dequeReusable...' with an argument of type '(String)'
on line var cell:UITableViewCell =
self.tableView.dequeueReusableCellWithIdentifier("cell") as
UITableViewCell
(sorry i'm extremely new to Xcode and swift)
when i click my UITableView in my storyboard, it is a table view cell with style Subtitle and identifier cell. (I may be doing this completely wrong, not sure)
I think I find the problem, try to take the word self of the dequeueReusableCellWithIdentifier call, like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel!.text = self.funlists[indexPath.row]
return cell
}
The reason been that you want to use the tableView coming from the function and not the tableView that belongs to the class.
Let me know if this solve your problem
Please define your cell identifier in storyboard.
I use Http get to get the json data and parse it.
It works fine so far and it shows the data on the UITableView.
I have a UITableCell in UITableView.
And the cell also has three UILabel-s in it like in the following picture.
And I have set the Identifier of TableViewCell to "Cell" like in the following picture.
I want to set "AAA" , "BBB" , "CCC" to the UILebel-s in UITableViewCell, but in the following code, I can not find any UILabel in UITableViewCell.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.
return cell
}
Should I need to connect the UILabel to Swift file?
How to find and set the text to UILabel of UITableViewCell?
Did I missing something?
Reason why you can not access the labels in the code is that you have no connection of labels with the code, by adding labels only on the default UITableViewCell just won't work.
I suppose you are using custom UITableViewCell? if not then use a custom cell and add three labels in the code and connect them on UI as outlets and in your code you can access them by using cell.label1 and so on
class CustomTableViewCell: UITableViewCell {
#IBOutlet var label1:UILabel!
#IBOutlet var label2:UILabel!
#IBOutlet var label3:UILabel!
}
In your main tableview class you could access them like as follows
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell
cell.label1.text = "AAAA"
return cell
}
Yes, you can do this by giving every label a unique tag and then access that label in the ViewController with the tag property then your cellforRowatIndexpath will change to following code :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? CustomTableViewCell {
if let label1 = cell.viewWithTag(1) as? UILabel { // 1 is tag of first label
label1.text = "AAAA"
}
if let label2 = cell.viewWithTag(2) as? UILabel { // 2 is tag of second label
label2.text = "BBBB"
}
if let label3 = cell.viewWithTag(3) as? UILabel { // 3 is tag of third label
label3.text = "CCCC"
}
return cell
}
return UITableViewCell()
}
Hope it will help you.
I have this code, that I copied out of the default MasterDetail App, yet when I run the program, it stalls on the line with the declaration of the cell. And I have no clue why.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let object = objects[indexPath.row].title as String
cell.textLabel.text = object
return cell
}
You haven't register cell to use on table view
I hope you forgot to do this.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
If you didn't register the cell for tableview, then it may return nil to cell.
Apple Says
let == Constant
var == variable values at any time
So using ? will consider as optional
you should have like this
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
//OR
//var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
//Your stuff
return cell
}
According to This tutorial from Apple, you may want to check the Table View on the story board.
Look for the attribute inspector for the Table View and verify that the "content" field is set to "Dynamic Prototypes". Updating this fixed it for me.
Also, while you have the hood open, verify that there is a single table cell in the table view, and make sure that it's "Identifier" field is set to the value you had in the tableView.dequeueReusableCellWithIdentifier method call (in your case "cell")