Swift Custom Cell creating your own Cell with labels - ios

I've just started to use Swift as a prorgamming language and i've run into a problem with Custom cells.
When i try to create custom cells, and then go forward and try to design them the way i need them ( with Style set to Custom ) everything looks good. Now i don't know how to put specific data into them, since all tutorials i found used the style option "basic" where they only have a text label to which they assign their data.
Now for me, when i "control drag" my labels into my code, i give them specific names such as "dateLabel" or "sourceLabel" in order to insert the data correctly.
now i'm not sure, and couldn't find any answers that worked, on how to recall my custom made labels so that i can assign my data to them...
Maybe someone of you could help me with this, since i'm pretty sure it's a simple problem but i coudln't find any resources to this ^^
hopefully the font isn't to small, i just wanted you guys to see the erros i get.
I used the following tutorial as a guide line, since it was the only one that worked just the way this guy did it: https://www.youtube.com/watch?v=0qE8olxB3Kk
I checked the identifier and he is set correctly and i can't find anything online on how i have to properly refer to my own labels with their correct names.
any help would be appreciated :)

Try the following steps:
Create a custom table view cell class that extends UITableViewCell. In my example, the custom table view cell class is called MyCustomTableViewCell.
Update your storyboard's cell so that it uses your custom table view cell class. Go to the Identity Inspector and set the Class value to the name of your custom table view cell class.
Update your storyboard's cell and give it a reuse identity value. Go to the Attributes Inspector and set the Identifier value. For example, I gave my cell an Identifier value of MyCustomCell.
Control drag the cell's labels into your new custom table view cell class (i.e., the MyCustomTableViewCell class).
Once you have done the above steps, you will be able to access the labels when you dequeue your cell in the tableView:cellForRowAtIndexPath: method. As the code snippet below shows, you will need to: 1) get the cell using the reuse identifier you established in the steps above and 2) cast to your custom table view cell class.
For example, here's what your custom table view cell would look like if you named it MyCustomTableViewCell. This is after you created the class and control dragged your labels into this class.
class MyCustomTableViewCell: UITableViewCell {
#IBOutlet weak var categoryLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var sourceLabel: UILabel!
#IBOutlet weak var titleLabel: UILabel!
}
Your ViewController could look like this:
// NOTE: I subclassed UITableViewController since it provides the
// delegate and data source protocols. Consider doing this.
class ViewController: UITableViewController {
// You do NOT need your UILabels since they moved to your
// custom cell class.
// ...
// Omitting your other methods in this code snippet for brevity.
// ...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Use your cell's reuse identifier and cast the result
// to your custom table cell class.
let article = tableView.dequeueReusableCellWithIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomTableViewCell
// You should have access to your labels; assign the values.
article.categoryLabel?.text = "something"
article.dateLabel?.text = "something"
article.sourceLabel?.text = "something"
article.titleLabel?.text = "something"
return article
}
}

Related

Cannot override mutable property 'collectionView' of type 'UICollectionView?' with covariant type 'GeminiCollectionView?'

I have had a custom collection view and everything worked fine. I wanted to include 'Gemini' to make beautiful animations when sliding my cells horizontally. I believe that the problem is not Gemini, I think the same would have happen with changing VC class to any other one, but I dont know how to solve this because I never faced with this before, is there a shortcut from this?
I have installed and imported the pod into code.
I had a UICollectionViewController but to work with gemini I needed to connect my collectionView from Storyboard to ViewController. Before that, I have put a class in Storyboard for my CollectionView to be GeminiCollectionView like in the image below:
After that, I changed a class for my CollectionView in ViewController, too, like in the image below and got those three errors:
In the later code, it doesnt show any errors:
viewDidLoad
collectionView.gemini
.rollRotationAnimation()
.degree(45)
.rollEffect(.rollUp)
cellForItemAt
self.collectionView.animateCell(cell)
scrollViewDidScroll
self.collectionView.animateVisibleCells()
willDisplay cell
if let cell = cell as? RatingCell {
//RatingCell inhertis the GeminiCell class
self.collectionView.animateCell(cell)
}
So, all the code is fine except the declaration as I mentioned at the beginning and here:
#IBOutlet var collectionView: GeminiCollectionView!
Waiting for random guys to unvote this :P
Solved
#IBOutlet var collectionView: GeminiCollectionView!
That line was a problem. Seems like you mustn't default Swift's name to override mutable property, which is collectionView for Collection Views and tableView for Table Views.
I changed that line to
#IBOutlet var collView: GeminiCollectionView!
...and the errors disappeared.

'Outlets cannot be connected to repeating content' in subclass of UICollectionViewCell

Xcode 9.2, Swift 4. For a cell in a Collection View Controller, I created a subclass named CollectionViewController. I linked the cell to this subclass. I created a Label in the cell on the Main.storyboard and linked it to the subclass like this :
class CollectionViewCell: UICollectionViewCell {
#IBOutlet weak var personName: UILabel!
}
Then i try to access to this label in the collectionView function inside my CollectionViewController class, subclass of UICollectionViewController, which is linked to the Collection View Controller where my cell is :
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
cell.personName.text = "text"
return cell
}
That's how i have this error : "The personName outlet from the UICollectionView to the UILabel is invalid. Outlets cannot be connected to repeating content."
I'd suggest you double check the "connections inspector" (the last tab on the panel on the right) for that control (and any other controls inside that cell). It sounds like something in the cell has a lingering outlet hooked up to the view controller. The connections inspector will help you identify that:
Make sure the view controller doesn't show up as one of the outlets. In the above example, I have "accidentally" created two outlets for this label, one to the cell subclass (which is correct) and one to the view controller (which is incorrect).
That will result in a compile-time error that says:
error: The customLabel outlet from the ViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.
If you delete the outlet connection between the cell and the view controller (or whatever non-cell class it's hooked up to), and this compile-time error will go away.
Note, the sentence before the "Outlets cannot be connected to repeating content" message will tell you precisely which outlet is causing the problem. You can even click on this error inside the "issues navigator" in the left panel and Xcode will jump to the storyboard and select the offending control (at which point you can directly open up the connections inspector) and find the offending outlet.
Additional note for similar error condition:
In the error message I got, along with the message similar to above, Xcode(ver 12.0) also provided the Object ID, which I referred to in the Identity Inspector tab to eradicate the cause of the error.
solution: From viewcontroller
kindly remove the IBoutlet of colllectionviewcell
. the issue mentions the invalid of your IBOutlet. so remove all subclass which has multi-outlet(invalids) and reconnect it.
The answer is already mentioned in another question

Xcode Error: Outlets cannot be connected to repeating content

After doing some searching and editing, I can't seem to find a solution to fixing this error. I'm trying to link my location search results with a table to display the search results in a list-form.
I have my map with the details button linked with a UIViewController called 'FirstViewController.' My results table is linked with a UITableViewController called 'ResultsTableViewController.' My prototype cells are linked with a UITableViewCell called 'ResultsTableCell' which is also where my outlets are located.
Here are the 2 separate errors:
Illegal Configuration: The nameLabel outlet from the ResultsTableViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.
Illegal Configuration: The phoneLabel outlet from the ResultsTableViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.
I've read other people's posts with the same problem, tried to fix them accordingly and I'm still getting the same error.
Here is the code for populating the cell, located in my ResultsTableViewController.
let cell = tableView.dequeueReusableCellWithIdentifier("resultCell", forIndexPath: indexPath) as! ResultsTableCell
// Configure the cell...
let row = indexPath.row
let item = mapItems[row]
cell.nameLabel.text = item.name
cell.phoneLabel.text = item.phoneNumber
return cell
}
The code in my ResultsTableCell class:
import UIKit
class ResultsTableCell: UITableViewCell {
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var phoneLabel: UILabel!
}
This message only occurs if you connect it to the view controller. As I have already commented, you probably did not delete the first connection outlet you've made to your view controller. Even if you delete the IBOutlet code from your view controller you still need to right click it and delete the old connection that probably still there. After deleting it the error message will go away.
this issue happen when you delete view from your class but still have reference in your view
here is example I remove back outlet reference from my class but my view still keep the reference Notice yellow rectangle just delete it by click at x
if you want to know how to reach this view , open your storyboard , right click at top left yellow it will show this dialog

Custom header of table view with IB

I would like to create a custom header for a table view section with interface builder. I cannot use the method titleForHeaderInSection because i need to display two labels. I used this instructions: Customizing Header and Footer of TableView in iOS8 but it does not work.
What I have done so far:
Create a custom class CustomTableCell which inherits from UITableViewCell
class CustomTableCell: UITableViewCell {
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var dateDescriptionLabel: UILabel!
}
Create a dynamic prototype cell in storyboard
Add an identifier
Connect the labels to the custom UITableViewCell
Implement viewForHeaderInSection
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell
headerCell.backgroundColor = UIColor.cyanColor()
headerCell.dateLabel.text = "Test date"
headerCell.dateDescriptionLabel.text = "Test date description"
return headerCell
}
When i run the app the section appears one second and then moves under the table cells and I get an error: no index path for table cell being reused.
What is wrong with this solution? I downloaded the project from the tutorial I have linked and it works there.
Your problem has its roots in how UIKit handles UITableViews.
To make sure table views are fast and responsive, even with a large amount of cells, the cells are re-used.
By calling tableView.dequeueReusableCellWithIdentifier("CustomTableCell") you are asking the tableView to give you a cell to reuse.
A lot of people have been using the reusable cells to design their headers/footers in Storyboards. Ever since iOS 7 Beta 5 this may lead to errors. This answer explains the situation well: What is the meaning of the “no index path for table cell being reused” message in iOS 6/7?
To design your own custom header/footer views I don't recommend using UITableViewCells. Instead you should create and design a custom UIView directly in your code. If you want to use Interface Builder you could create a .xib or create a view in your Storyboard that is not a subview of the actual controller's view.

"Use of unresolved identifier" with Swift

I am rewriting one of my apps in Swift, that displays live weather data for South Kohala. Love Swift so far!
I'm having one small problem that is holding things up. I have a tab bar based app, iPad only.
One of my tabs is a UIViewController with a TableView added to it in the storyboard to display NOAA forecasts. I have a data class Data that retrieves the list from our server and creates a constant List in viewDidLoad. It is an array of arrays with four strings in each subarray.
I have verified that it's there, as I can create a constant: List[0] and println all the strings.
However, when I go to use it to populate the table view, I get a "Use of unresolved identifier" error. If instead, I try setting the cell Title to "Test" that works OK.
This seems like a scope issue, but as the constant is created within the class, I just don't understand why it's not visible in the tableView function. I've looked at dozens of posts on this, but nothing I have found seems to help. Here is the code:
import UIKit
class ForecastsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var forecastsTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let instanceofData: Data = Data()
let list = instanceofData.forecastsList() as NSArray
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
var forecastList:NSArray = list[indexPath.row] //selects one of the arrays from List
cell.textLabel?.text = forecastList[0] as string
return cell
}
It is because List is purely local to the inside of viewDidLoad; where you've declared it, other methods cannot see it.
Contrast, for example, forecastsTable, which is declared outside any method, which means that any method can see it.
This behavior is called "scope" and is crucial to any programming language. Variables declared inside a method are neither visible outside it nor do they persist when that method has finished running. Thus, in your example, not only is List not visible when cellForRowAtIndex runs, it doesn't even exist when cellForRowAtIndex runs - it has been destroyed long before.

Resources