UITableViewCell solution for the reuse problem in iOS

Source: Internet
Author: User

UITableViewCell Reuse

    • In order to ensure that Tableviewcell can be executed efficiently, the mechanism of reusing queues is introduced in Objective-c, and the ghosting phenomenon is also a common problem in reusing queues, so how to solve this problem? Several workarounds are given below.

The first method of solution

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{     *subviews = cell.contentView.subviews;       for inch subviews) {        [view Removefromsuperview];    }}  

The above just lists the location of the method implementation, and does not write all the code. The above implementation method is to cell.contentview the above sub-view all out, remove them one by one, this is a way to solve the problem , if the sub-view is too much, each time the reuse of the one by one of the view will be removed in the execution of the program to create a problem.

The second way of solving

  • We would like to think, since all removed after one by one initialization using this method is not very high in execution efficiency, if in addition to the first application of memory space, in other cases, before using the first to determine whether the current sub-view exists, if there is to replace the above content does not avoid the problem of repeated application of memory?
  • -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{nsdictionary*dicuserinfo = [Self.statuslist[indexpath.section] Objectforkey:@"User"]; Nsdictionary*statusinfo =Self.statuslist[indexpath.section]; Nsuinteger Widthspace=5; //cell's Reuse queue    StaticNSString *cellidentifier =@"Statuscell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Nil = =cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; //The following is a definition of the microblog headUiimageview *imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (5,5, *, *)]; Imageview.tag= +;        [Cell.contentview Addsubview:imageview]; UILabel*labelscreenname = [[UILabel alloc] Initwithframe:cgrectmake (Cgrectgetmaxx (imageview.frame) +widthspace,2, -, -)]; Labelscreenname.font= [Uifont systemfontofsize:12.0f]; Labelscreenname.tag=1001;        [Cell.contentview Addsubview:labelscreenname]; UILabel*labelcreatetime = [[UILabel alloc] Initwithframe:cgrectmake (Cgrectgetmaxx (imageview.frame) +widthspace, Cgrectgetheight (Labelscreenname.frame) +2, Max, -)]; Labelcreatetime.font= [Uifont systemfontofsize:12.0f]; Labelcreatetime.tag=1002;        [Cell.contentview Addsubview:labelcreatetime]; UILabel*labelsource = [[UILabel alloc] Initwithframe:cgrectmake (Cgrectgetmaxx (labelcreatetime.frame) +widthspace, Cgrectgetheight (Labelscreenname.frame) +2, -, -)]; Labelsource.tag=1003; Labelsource.font= [Uifont systemfontofsize:12.0f];    [Cell.contentview Addsubview:labelsource]; } Uiimageview*imgview = (uiimageview*) [Cell.contentview Viewwithtag: +]; Nsurl*imgurl = [Nsurl urlwithstring:[dicuserinfo objectforkey:@"Profile_image_url"]];    [Imgview Setimagewithurl:imgurl]; //Weibo user nameUILabel *screenname = (uilabel*) [Cell.contentview Viewwithtag:1001]; Screenname.text= [Dicuserinfo Objectforkey:@"Screen_name"]; //Weibo creation TimeUILabel *createtime = (uilabel*) [Cell.contentview Viewwithtag:1002]; NSString*strdate = [StatusInfo objectforkey:@"Created_at"]; NSDateFormatter*dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Setdateformat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"]; NSDate*datefromstring =[Dateformatter datefromstring:strdate]; Nstimeinterval interval=[datefromstring Timeintervalsincenow]; Createtime.text= [NSString stringWithFormat:@"%d minutes ago", ABS (int) interval/ -)]; //Weibo sourcesUILabel *labelsource = (uilabel*) [Cell.contentview Viewwithtag:1003]; NSString*xmlsourcestring = [StatusInfo objectforkey:@"Source"]; Nsdictionary*dicsource =[Nsdictionary dictionarywithxmlstring:xmlsourcestring]; Labelsource.text= [Dicsource Objectforkey:@"__text"]; returncell;}

  • The second solution is to use the code of the microblog as an example, by adding a tag to all the sub-views above the Cell.contentview, because tag is the unique identifier of the view, and when using the reuse queue, it only needs to be fetched through the tag view. Change what is displayed by changing the contents of the sub-view. The efficiency of this approach is obviously much higher than the first solution.

A third way of solving

    • Understanding object-oriented people understand that this can be packaged in a way that can do the same thing. In fact, this approach is similar to the second approach, is to create a new custom class to implement the above functions, such as customizing a cell class, you can encapsulate the above functions in a custom class, using a custom class can also be implemented, but the custom class is generally only fixed classes to encapsulate . We are interested in experimenting with this method.

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.