Basically I am trying to get a value from a custom table view cell, as I have made 5 prototype cells - each with a different tableViewCell class. However, what I am trying to do is to read the data from the cell above. I have called my classes: TableViewCell1, TableViewCell2 etc... So on the cellForRowAtIndexPath, I have the code like this:
if indexPath.row == 1 {
let cell2 = tableView.dequeueReusableCellWithIdentifier("secondCell", forIndexPath: indexPath) as! TierCell2
return cell2
}
This works, but I have also tried to do it like this, to be able to access the value above (with no luck) - which only results in an optional value error.
let cell1:TableViewCell1 = TableViewCell1()
cell2.PriceText.text = cell1.PriceText.text
Any ideas on how I might accomplish this?
I suggest you then try creating one custom table view cell with the 5 price tier labels that you want to display. Then in the cellForRowAtIndexPath you just deque a cell as the custom class and fill in all the tier information that you already have in your dictionary and return that cell.
Related
I am new in swift, the issue I am facing is I have a collection view of height 2000. when I scroll down to the bottom, the indexPath.row gets changed due to which the logic gets disturbed. I understood that this happens due to the reusability of cells but how should I stop this. This is how I am initializing cellForItemAt.
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "analysis", for: indexPath) as? analysisCollectionViewCell
if goalData.notesArr.count == 0 {
cell?.viewNotes.isHidden = true
cell?.constraintHeightNotesView.constant = 0
self.constraintHeightMainView.constant = self.constraintHeightMainView.constant - 400
}else{
cell?.viewNotes.isHidden = false
cell?.constraintHeightNotesView.constant = 470
}
I am changing the height of a few components(cell?.constraintHeightNotesView.constant) as well as of my mainView also(self.constraintHeightMainView.constant).
Please, edit the question so that We can see your code in full
This cell initializing method is correct
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "analysis", for: indexPath) as? analysisCollectionViewCell
But I can't understand why you need an collection view height 2000?
If you want to setup layout the collection view in full screen, You need to set constraints of collection view to the edges of the main view of your ViewController
if you collection view have a many elements, you will be able to scroll it.
This functionality is available without additional configuration.
I have 3 Labels in my prototype cell of a tableview. I want to connect them to my code and give them a name that I can use them for the cellforrow method. I connected them to the code ,but I cannot use them in this method. Does anyone know how to this?
1.Create the custom table view cell with a separate class
2.Create the UILabel as the property of custom table view cell
3.Then you can use the below code for access the UI label of your custom table view cell
let customCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? CustomTableViewCell
customCell.yourLabel.text = “Success”
I have a question about cell in swift. Let's imagine that I have a cell with 20 rows. The cell is selectable right now, but the thing I want is that, when user selects 20 row it should be editable. And the value which enters the user should be saved in some variable. Is it somehow possible? Thank's in advance!
If you want 20 cells in tableView, then
You have to make nib of a cell, and make textfield in cell, and also create outlet in nib, (you can also create prototype cell)
Then in cellForRow AtIndexPath makes textField delegate as self (to class), in that class you have to implement textField delegate, and then you can access textFields as:
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView(tableView cellForRowAt: IndexPath) as! MYCELL
let text = cell.textField.text
Sorry for my bad english,
I think it will be helpful
I have a UITableviewCell with multiple lines of text. I am trying to figure out how to make each line a different font size.
Here is the code I am using to populate the table cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("eventCell")!
cell.textLabel!.textAlignment = NSTextAlignment.Center
cell.textLabel!.numberOfLines = 0
cell.textLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.textLabel!.text = self.eventsToday[indexPath.row].eventTitle + "\n" + self.eventsToday[indexPath.row].eventPlaceName
return cell
}
Is there a way to reduce the font size after the "\n" line break so that the string labeled "eventPlaceName" will show up as smaller than the one labeled "eventTitle?"
Use attributedText property of textLabel object.
Using attributedText you can format your text.
For more, see NSAttributedString
Is there a way to reduce the font size after the "\n" line break so
that the string labeled "eventPlaceName" will show up as smaller than
the one labeled "eventTitle?"
This sounds exactly like your table view cell needs TWO SEPARATE labels:
One for the "Event title"
One for the "Event place"
I mean, you could use an attributed string, but clearly these are two separate pieces of information and you have a repeating pattern here. Two labels sounds like the best practice / less headaches down the road, in my opinion.
How to add an extra label:
Subclass UITableViewCell (Let's call the custom subclass CustomTableCell in the code below).
Set the class of your prototype cells to your custom subclass, in the storyboard. Also set the reuse identifier (here I used "YourCellIdentifier").
Forget the original label (it doesn't show in the storyboard and therefore you couldn't position your second label relative to it); instead add TWO new labels to the cell, and setup outlets with the custom cell subclass. Name the properties (e.g.) eventTitleLabel and eventPlaceLabel. Setup any layout constraints you may need, and configure the font sizes, colors, etc.
At runtime, set the labels' text like so:
let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier", forIndexPath: indexPath) as! CustomTableCell
// (forced cast shouldn't fail f identifier is registered properly)
cell.eventTitleLabel.text = "Event Title XXXX"
cell.eventPlaceLabel.text = "Event Place YYYY"
return cell
I added a table view to my UIViewController. then i added a label to my custom cell inside the table view. i tried to update labels inside the tableview by using tags. seems it does not working.
this is my code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
When i call the function always it does not read the tag and prints "fail". i have assigned correct tag value to my label also.
here i have attached a image of my label details in attribute inspector
Please help me the fix the issue.
As #TheAppMentor said code looks fine. Make sure you entered correct cell identifier. Then make sure you connected your view controller to correct class (it's my common mistake). Also make sure that something else in this cell hasn't the same tag. You can also print all subviews to check what's inside.
Your cell is not knowing about your label inside (the one with 500 tag). You are not providing a custom cell, because of this line:
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
This is a predefined cell. As of documentation you can have predefined cells or custom ones (obtained by subclassing UITableViewCell):
"When creating cells, you can customize them yourself or use one of several predefined styles. The predefined cell styles are the simplest option. With the predefined styles, the cell provides label and image subviews whose positions and styling are fixed. ... To set the text and images of the cell, use the textLabel, detailTextLabel, and imageView properties."
If you want to go predefined:
If you want to just put the text onto cell use (I see this one is commented on first place).
cell.textLabel?.text = object["quantity"]
Go custom
Extend UITableViewCell on a separate swift file. Do your bindings here and work with your storyboard in parallel. Assign the custom class to your cell. Also, change dequeue:
let cell: MyCustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath) as! MyCustomTableViewCell