iOS self-defining class (UIView) code generates a simple UITableViewCell

Source: Internet
Author: User

Because there is a large number of UITableViewCell in a project that need to be written, the style is almost identical to the text: Display content .

I do not bother to write UITableViewCell class suspicion is not necessary, in the method Tableview:cellforrowatindexpath handwriting is cumbersome. Wrote a UIView class on the package change.

Project: Click to download

Idea: First because the text description and the width of the display content is fixed, and then the cell row (cell can contain more than one line) height is text description and display content The height required is higher than the two. The next line is the height accumulation and repetition. The upper and lower end of the cell are given a height. At the bottom, draw an interval.

First,UITableViewCell own definition class Commontableviewcellview

1. CommonTableViewCellView.h

#import <uikit/uikit.h>//Pass the parameters of their own active layout uitableviewcell, style:  Lable:value use method: References Viewcontroller@interface commontableviewcellview:uiview{    uicolor *_cellviewcolor;//cell color, reserved, write a method when required    cgfloat _labelspace;// lable width, reserved. Write a method    cgfloat _viewheight when required;} @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_; Transverse cutting 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 self-defining 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.