Create a custom uitableviewcell object in the XIB file on the storyboard.

Source: Internet
Author: User

After ios5.0, you can directly draw custom uitableviewcell in uitableview, and do not customize uitableviewcell. Remember that the identifier (identifier) in the storyboard must be consistent with that in the Code. It is intuitive and convenient to use. It seems that the times of reading from the XIB (NIB) files are gone forever. But suppose we have uitableviewa and uitableviewb. We have defined several uitableviewcells in uitableviewa. Now we need to reuse these ready-made uitableviewcells in uitableviewb. The ideal method is to create identifier in uitableviewb, but no, because dequeuereusablecellwithidentifier, which is normally used in uitableviewa, returns nil in uitableviewb.

Here we first understand the role of identifier. Each uitableviewcell has a reuseidentifier (reuse ID). When reuseidentifier is configured, uitableview puts this object together with the reuseidentifier into its own object pool, when you create a uitableviewcell object again, uitableview will first match whether the object has been created in the object pool. If this object is created, it will not be created again (if it is created repeatedly, when there are many uitableview lines, IOS memory consumption), directly return the previously created object. Therefore, the uitableviewcell created by uitableviewa has been reused in the uitableviewa Object pool according to the identifiera defined by us. However, its function is only in the uitableviewa Object pool. When we call uitableviewb, its
List of uitableviewcell objects is empty, so its return value must be empty. Here, I also boldly guess that when storyboard loads uitableviewa, it will automatically load its uitableviewcell (custom) interface resources once, so we can directly call dequeuereusablecellwithidentifier :.

Since it cannot be called directly, we can only go back to the old path through the XIB file. In xcode, we will continue to create the XIB file, subclass uitableviewcell, and associate it with the XIB file, when loading in uitableview, I gave me a piece of code

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {nsstring * vertex; uitableviewcell * cell; // uitableviewcell into * lableswitchcell; uinib * n; If ([indexpath section] = 0) {cellidentifier = @ "lableswitchxibcell"; lableswitchcell = [tableview partition: cellidentifier]; If (lableswitchcell = nil) {nsarray * _ nib = [[nsbundle mainbundle] loadnibnamed: @ "lableswitchcell" Owner: Self options: Nil]; lableswitchcell = [_ nib objectatindex: 0]; // use this code to complete the reuseidentifier setting of lableswitchxibcell. // This setting is easy to ignore. If this segment is not found, dequeuereusablecellwithidentifier is returned when lableswitchxibcell is loaded again: the value is still nil n = [uinib nibwithnibname: @ "lableswitchcell" Bundle: [nsbundle mainbundle]; [self. tableview registernib: N forcellreuseidentifier: @ "lableswitchxibcell"] ;}}

It should be noted that you need to call the registernib: forcellreuseidentifier: function added to ios5.0. Finally, we will summarize the reuse ID of uitableviewcell, which does not use a Global Object pool, it is a reusable identifier for a single uitableview instance. If the demo is a little large, I will not release it. Some kids shoes will ask me to send it again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.