IOS Runtime Property Association Implementation table Edit text

Source: Internet
Author: User

To achieve in the table editing text, the table reminds me of CollectionView, text reminds me of TextView, do think a long time how to get the editor is which TextView, to get the corresponding Indexpath ah, Thinking about the button in the cell before the implementation of block, in the custom cell to add a property indexpath, you can think of a textview to customize to write a class that would be too much trouble. Just suddenly think of the attribute management that I have heard before, I have the opportunity to use the next, bright is not bad.

Sometimes instances of a class may be created by some mechanism, and developers cannot make this mechanism create instances of subclasses that they write. You can associate many other objects with an object. These objects are distinguished by "keys". When storing object values, you can specify a "storage Policy" to maintain the appropriate "memory management semantics." The storage policy is defined by an enumeration named Objc_associationpolicy. If the associated object is a property, then it will have the corresponding semantics. TD valign= "Top" and equivalent @property property
Association type
objc_association_assign as Sign
objc_association_retain_nonatomic nonatomic,retain
objc_association_copy_nonatomic nonatomic,copy
objc_association_retain retain
objc_association_copy copy
The following methods are used to manage associated objects: ①void objc_setassociatedobject (ID object, void *key, id value, Objc_associationpolicy policy); This method sets the associated object value for an object with the given key and policy. ②void Objc_getassociatedobject (ID object, void *key); This method obtains the corresponding associated object value from an object based on the given key. ③void objc_removeassociatedobjects (ID object); This method removes all associated objects from the specified object. 4. The key used to set the associated object is an "opaque pointer", meaning that the data structure pointed to is not limited to a specific type of pointer. When you set the associated object value again, if you want to match two keys to the same value, the two must be exactly the same pointer. It's not like nsdictionary. Nsdictionary that "isequal:" returns Yes, the two are the same. Therefore, when you set the associated object value, you typically use a static global variable to do the key. Code:
#import "ViewController.h"//property associations belong to the dynamic runtime#import<objc/runtime.h>//Set KeyConst StaticNSString *key=@"Indexpath";@interfaceViewcontroller () <uicollectionviewdatasource,uicollectionviewdelegate,uicollectionviewdelegateflowlayout, Uitextviewdelegate>{cgfloat _imgheight; Uicollectionview*_mcollectionview;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];    [Self.view addsubview:[self Createcollectionview]; }//Create CollectionView-(Uicollectionview *) createcollectionview{//UicollectionviewdelegateflowlayoutUicollectionviewflowlayout *layout =[[Uicollectionviewflowlayout alloc] init]; _imgheight= (Self.view.frame.size.width- -)/4; Layout.itemsize= Cgsizemake ((Self.view.frame.size.width- -)/4, -); Layout.minimumlinespacing=Ten; Layout.minimuminteritemspacing=Ten; Layout.sectioninset= Uiedgeinsetsmake (Ten,Ten,Ten,Ten); _mcollectionview=[[Uicollectionview alloc] InitWithFrame:self.view.bounds collectionviewlayout:layout]; _mcollectionview.Delegate=Self ; _mcollectionview.datasource=Self ; _mcollectionview.backgroundcolor=[Uicolor Whitecolor]; [_mcollectionview Registerclass:[uicollectionviewcellclass] Forcellwithreuseidentifier:@"Cell"]; return_mcollectionview;}//uicollectionviewdatasource,uicollectionviewdelegate-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return 2;}-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{if(section==0) {        return Ten; }    return  -;}- (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) indexpath{NSLog (@"%@", Indexpath);}-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{Uicollectionviewcell*cell = [CollectionView dequeuereusablecellwithreuseidentifier:@"Cell"Forindexpath:indexpath]; Uitextview*textview=[[uitextview Alloc]initwithframe:cgrectmake (0,0, _imgheight, -)]; TextView.layer.borderWidth=2; TextView.layer.borderColor=[Uicolor Redcolor].    Cgcolor;        [Cell.contentview Addsubview:textview]; //Property Association SettingsObjc_setassociatedobject (TextView, key.    Utf8string, Indexpath, Objc_association_retain); TextView.Delegate=Self ; returncell;}//uitextviewdelegate- (void) Textviewdidendediting: (Uitextview *) textview{//Property Association GetNsindexpath *indexpath=Objc_getassociatedobject (TextView, key.    utf8string); NSLog (@"%@", Indexpath); }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

IOS Runtime Property Association Implementation table Edit text

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.