Fast custom cell creating your own shortcut cell

I was just starting to use Swift as the prorgamming language and I had a problem with custom cells.

When I try to create custom cells, and then I move forward and try to create them the way I need them (with custom style setting) everything looks good. Now I don’t know how to insert specific data into them, since all the textbooks I found used the "basic" style option, where they only have a text label to which they assign their data.

Now for me, when I “control the drag and drop” of my tags into my code, I give them specific names, such as “dateLabel” or “sourceLabel”, to insert the data correctly.

now I'm not sure, and could not find the answers that worked, on how to remember my user-made shortcuts so that I could assign them my data ...

Perhaps some of you could help me with this, since I am sure that this is a simple problem, but I will not find any resources for this ^^

enter image description here

We hope that the font is not small, I just wanted you guys to see the ero I get.

I used the following tutorial as a guideline, as he was the only one who worked the way this guy did this: https://www.youtube.com/watch?v=0qE8olxB3Kk

I checked the identifier and it is configured correctly, and I cannot find anything on the Internet about how I should correctly refer to my own labels with their correct names.

any help would be appreciated :)

+5
source share
1 answer

Try the following:

  • Create your own table cell class that extends UITableViewCell . In my example, the cell class of the custom table view is called MyCustomTableViewCell .

  • Update the storyboard cell to use your own table cell class. Go to the Identity Inspector and set the class value for the name of your class of table cells. Set custom cell class in identity inspector

  • Refresh the storyboard cell and set it to reuse value . Go to the Attributes Inspector and set the value to Identifier. For example, I gave my cell the value Identifier MyCustomCell . Set your cell reuse id

  • Control the drag and drop of cell labels to your new table cell class with a list of tables (i.e., the MyCustomTableViewCell class).


After completing the above steps, you can access the shortcuts when you deactivate your cell in the tableView:cellForRowAtIndexPath: method. As shown in the code snippet below, you need to: 1) get the cell using the reuse identifier set in the above steps, and 2) apply the user-defined table type to the cell class.

For example, here's what your custom table view cell looks like if you named it MyCustomTableViewCell . This is after you created the class, and the 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 might 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 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 } } 
+17
source

Source: https://habr.com/ru/post/1233207/


All Articles