Ios custom class (UIView) code to generate a simple UITableViewCell
Because a large number of uitableviewcells need to be written in a project, the style is almost a text description: The display content is like this. It is unnecessary to write the UITableViewCell class. Handwriting in the tableView: cellForRowAtIndexPath method is cumbersome. Write a UIView class for encapsulation changes.
Project: Click to download
Concept: first, the text description and display content are fixed in width, and then one Cell line (Cell can include multiple lines) the height is the height required by the text description and display content. The next row is the high accumulation and repetition. The upper and lower ends of the Cell are given a height, and the lower end is given an interval.
1. UITableViewCell custom class CommonTableViewCellView
1. CommonTableViewCellView. h
# Import
// Automatically layout UITableViewCell by passing parameters. Style: lable: Value usage: see viewController @ interface CommonTableViewCellView: UIView {UIColor * _ cellViewColor; // cell color, reserved item, if required, write the CGFloat _ labelSpace method; // lable width and retention items. If required, write the CGFloat _ viewHeight method.} @ 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 _ 100366f # define contentWidthSpace _ self. frame. size. width-labelSpace _-leftSpace _-rightSpace _ # define outer _ 20366f # define leftSpace _ 20366f # 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 objectAtIndex: I] text: [valueArray objectAtIndex: I];} self. viewHeight + = topBottomSpace _; // UIImageView x imgView_H = [[UIImageView alloc] initWithFrame: CGRectMake (0, self. viewHeight-1, self. frame. size. width, 1)]; imgView_H.backgroundColor = [UIColor colo RWithRed: 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, 20000000f); CGSize size = [text sizeWithFont: [UIFon T systemFontOfSize: 13.0f] constrainedToSize: constraint lineBreakMode: UILineBreakModeWordWrap]; return size. height> height? Size. height: height;} // row-(CGFloat) rectUIView :( CGFloat) height labelText :( NSString *) text {CGFloat textValueHeight = [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
Ii. Call UIViewController
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [self tableView: tableView cellForRowAtIndexPath: indexPath]; CGFloat height = cell. frame. size. height; return height;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "Cell "; UITableViewCell * cell = [tableView failed: CellIdentifier]; if (cell = nil) {cell = [[[UITableViewCell alloc] initWithStyle: descrireuseidentifier: CellIdentifier] autorelease]; UIView * view = [[UIView alloc] init] autorelease]; CGFloat viewHeight = 0.0f; for (int I = 0; I <5; I ++) {NSMutableArray * keyArray = [NSMutableArray arrayWithObjects: @ "text description 1:", @ "text description 2:", @ "I feel at ease when I know you are not doing well. 3 :", nil]; NSMutableArray * valueArray = [NSMutableArray arrayWithObjects: [NSString stringWithFormat: @ "random data % d", arc4random_uniform (100)], [NSString stringWithFormat: @ "Life is like a box of chocolate and you never know what you will get % d", arc4random_uniform (100)], [NSString stringWithFormat: @ "random data % d ", arc4random_uniform (100)], 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 ;}
3. Image and truth