iOS custom Class (UIView) code generates a simple UITableViewCell

Source: Internet
Author: User

Since there is a large number of UITableViewCell in a project that need to be written, the style is almost text-like : Display content . I do not bother to write UITableViewCell class is not necessary, in the method Tableview:cellforrowatindexpath handwriting and cumbersome. Wrote a UIView class on the package change.

Project: Click to download

Idea: First, because the text introduction and the width of the display content is fixed, and then the cell line (cell can include multiple lines) height is text description and display content The height required is higher than the two. The next line is the highly accumulative repetition, the top and bottom of the cell are given a height, and the bottom is drawn at a distance.

First,UITableViewCell custom class Commontableviewcellview

1. CommonTableViewCellView.h

#import <uikit/uikit.h>//pass parameter automatic layout uitableviewcell, style:  lable:value usage: reference viewcontroller@interface commontableviewcellview:uiview{    uicolor *_cellviewcolor;//cell color, keep items, write a method when needed    cgfloat _labelspace;// lable width, reserved items, write a method    cgfloat _viewheight when needed;} @property (Nonatomic,retain) Uicolor *cellviewcolor; @property (nonatomic,assign) cgfloat labelspace; @property ( nonatomic,assign) cgfloat viewheight;-(ID) initWithFrame: (CGRect) frame Keyarray: (nsarray*) Keyarray ValueArray: ( nsarray*) ValueArray; @end

2.commontableviewcellview.m

#import "CommonTableViewCellView.h" #define Topbottomspace_ 10.0f#define labelspace_ 100.0f#define contentwidthspace_ Self.frame.size.width-labelspace_-Leftspace_-rightspace_#define contentheightspace_ 20.0f#define leftSpace_ 20.0f# Define Rightspace_ 5.0f@implementation commontableviewcellview@synthesize cellviewcolor = _cellViewColor; @synthesize    Labelspace = _labelspace; @synthesize viewheight = _viewheight;-(void) dealloc{self.cellviewcolor = nil; [Super Dealloc];} -(ID) initWithFrame: (CGRect) frame Keyarray: (nsarray*) Keyarray ValueArray: (nsarray*) ValueArray;    {self = [super Initwithframe:frame];        if (self) {self.labelspace = Labelspace_;                Self.cellviewcolor = [Uicolor Clearcolor];        Self.viewheight = Topbottomspace_; int count = Keyarray.count>valuearray.count?        KeyArray.count:valueArray.count; for (int i = 0;i < count; i++) {self.viewheight = [self rectUIView:self.viewHeight labeltext:[keyarray obje CtatindeX:I] Text:[valuearray objectatindex:i];        } self.viewheight + = Topbottomspace_; Horizontal Split Line Uiimageview *imgview_h = [[Uiimageview alloc]initwithframe:cgrectmake (0, Self.viewheight-1, self.frame.si        Ze.width, 1)];        Imgview_h.backgroundcolor = [Uicolor colorwithred:221/255.0f green:221/255.0f blue:221/255.0f alpha:1.0];        [Self addsubview:imgview_h];        [Imgview_h release];    [Self setframe:cgrectmake (0, FRAME.ORIGIN.Y, Self.frame.size.width, Self.viewheight)]; } return self;}  Reset Height-(cgfloat) Resizeviewheight: (NSString *) Text width: (cgfloat) Width height: (cgfloat) height{cgsize constraint =    Cgsizemake (width, 2000.0f); Cgsize size = [text Sizewithfont:[uifont systemfontofsize:13.0f] Constrainedtosize:constraint lineBreakMode:    Uilinebreakmodewordwrap]; return size.height>height?size.height:height;} Line-(CGFloat) Rectuiview: (cgfloat) Height LabelText: (nsstring*) labelText text: (nsstring*) text{cgfloat Textvalueheig HT = [selF resizeviewheight:text Width:contentwidthspace_ Height:contentheightspace_];    CGFloat labeltextheight = [self resizeviewheight:labeltext width:self.labelSpace height:contentheightspace_]; CGFloat cellheight = textvalueheight>labeltextheight?        Textvalueheight:labeltextheight;    UILabel *label = [self Rectuilabel:labeltext rect:cgrectmake (leftspace_, height, self.labelspace, cellheight)];        [Self Addsubview:label]; UILabel *textvaluelabel = [self Rectuilabel:text rect:cgrectmake (self.labelspace + leftspace_, height,    Contentwidthspace_, Cellheight)];        [Self Addsubview:textvaluelabel]; return height + cellheight;}     Column-(UILabel *) Rectuilabel: (NSString *) text rect: (cgrect) rect{UILabel *label = [[UILabel alloc] initwithframe:rect];    Label.backgroundcolor = Self.cellviewcolor;    Label.textalignment = Uitextalignmentleft;    Label.linebreakmode = Uilinebreakmodewordwrap;    Label.numberoflines = 0;    Label.font = [Uifont systemfontofsize:13.0]; LabeL.text = text; return [Label Autorelease];} @end

Second, Uiviewcontroller call

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell *ce    ll = [self Tableview:tableview cellforrowatindexpath:indexpath];    CGFloat height = cell.frame.size.height; return height;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {static Nsstri    ng *cellidentifier = @ "Cell";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {cell = [[[] UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellid                Entifier] autorelease];        UIView *view = [[[[UIView alloc] init] autorelease];        CGFloat viewheight = 0.0f; for (int i=0; i < 5; i++) {Nsmutablearray *keyarray = [Nsmutablearray arraywithobjects:@] Text Introduction            1: ", @" Text Introduction 2: "@" I know you're not well, I feel relieved. 3: "nil"; Nsmutablearray *valuearray = [Nsmutablearray arraywithobjects:[nsstring stringwithformat:@ "Random data%d", Arc4random_uniform (],[nsstring) stringwithformat:@ "Life is like a box of chocolates you never know what you're going to get%d", arc4random_            Uniform (+)],[nsstring stringwithformat:@ "Random data%d", arc4random_uniform (+)], nil]; Commontableviewcellview *cellview = [[[Commontableviewcellview alloc] Initwithframe:cgrectmake (0, ViewHeight,            Self.view.frame.size.width, 0) Keyarray:keyarray Valuearray:valuearray] autorelease];            Viewheight + = Cellview.viewheight;        [View Addsubview:cellview];        } [View Setframe:cgrectmake (0, 0, Self.view.frame.size.width, viewheight)];        Cell.accessoryview = view;        [Cell setframe:cgrectmake (0, 0, Self.view.frame.size.width, viewheight)];    Cell.selectionstyle = Uitableviewcellselectionstylenone; } return cell;

Three, there is a picture of the truth


iOS custom Class (UIView) code generates a simple UITableViewCell

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.