I am using UITextField in UITableViewCell and assign tags for each textFields. But after scrolling the tableView, the tags of the textFields are changed. I am using swift 3. How do I manage to make the textFields' tags constant when scrolling? I am not using storyboard.
In Swift 3 assign the textField tag as indexPath.row
Try this code:-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReuseIdentifer", for: indexPath) as UITableViewCell
cell.textField.tag = indexPath.row
}
you can set the tag property to the indexPath.row, and it will work fine.
Just as follows,
In cellForRowAtIndexPath: write the following:
let cell = tableView.dequeueReusableCell(withIdentifier:"ReuseIdentifer", for: indexPath) as! UITableViewCell
cell.textField.tag = indexPath.row
Related
My table have the functionality to delete a row, but when add a new row is writting two labels in the same place, a label above of the other label
a label belong to the deleted row and the other label belong to the new label when add a new row
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = UITableViewCell()
tableView.register(newCell.self, forCellReuseIdentifier:"newCell");
cell = tableView.dequeueReusableCell(withIdentifier: "newCell", for: indexPath) as! newCell
cell.textLabel.text= value
return cell
}
}
First of all, register your cell outside the cellForRowAt indexPath function, preferably where you are adding your tableView.
For this instance, register it in viewDidLoad
So
tableView.register(newCell.self, forCellReuseIdentifier:"newCell")
goes into viewDidLoad
for the function, try this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "newCell", for: indexPath) as! newCell
cell.textLabel.text= value
return cell
}
Hope this helps. Also, provide more context to what is going wrong and what is the intended behaviour.
I am trying to make a custom table view cell.
If I do this:
class TableViewCell: UITableViewCell {
#IBOutlet weak var cellBackgroundImage : UIImageView!
}
And:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
cell.cellBackgroundImage.backgroundColor = UIColor.white
cell.cellBackgroundImage.layer.masksToBounds = false
cell.cellBackgroundImage.layer.cornerRadius = 5
let event = self.fetchedResultsController.object(at: indexPath)
self.configureCell(cell, withEvent: event)
return cell
}
I obtain a white rounded cell background. Easy. And I can use the original cell.textLabel.text.
Perfect.
But, if I want to do something more complex:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? TableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell") as? TableViewCell
cell?.cellBackgroundImage.backgroundColor = UIColor.white
cell?.cellBackgroundImage.layer.masksToBounds = false
cell?.cellBackgroundImage.layer.cornerRadius = 5
self.configureCell(cell!, withObject: object)
}
And at the same time to use the original table view properties as UITableViewCellStyle.subtitle and cell.accessoryView, the app crashes or shows the wrong output.
THIS MEANS THAT I MUST USE A FULL CUSTOM CELL WITH MORE OUTLETS TO REPLACE THE ORIGINAL ELEMENTS AS UITableViewCellStyle.subtitle and cell.accessoryView ???
I will express it in a different way:
Can I use a custom tableview cell only for one purpose (like the rounded background) and use the original elements such as the subtitle style and the accesory view?
In afirmative case, how?
I have a UITableView set up with cells: (no conflicting constraints)
Storyboard and Constraints
Looks like:
Pre-refresh
However, whenever I refresh using
self.tableView.reloadData()
the constraints seem to change which changes the cell into:
Post-refresh
Any suggestions as to why this happens?
Code for cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CaptureCell
let capture = tabBar?.captureArray[indexPath.row]
cell.capsuleImage.image = capture?.image
return cell
}
The cell identifier is "cell" and I have CaptureCell which is a subclass of UITableViewCell.
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 am adding cell like below. Before selecting cell, only textLabel is able to be seen, after selection detailTextLabel getting appear too. Why detailTextLabel not on screen from beginning?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ooo", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.textColor = UIColor.whiteColor()
cell.textLabel!.text = "asdf1"
cell.detailTextLabel!.textColor = UIColor.whiteColor()
cell.detailTextLabel!.text = "asdf2"
return cell
}