UITableView (simple construction of Weibo interface)

Source: Internet
Author: User

Design a micro-blog interface through MVC design patterns

1.m--model

1) Data Model

1> statement

#import <Foundation/Foundation.h>

@interface Czstatus:nsobject

User picture

@property (nonatomic, copy) NSString *icon;

User name

@property (nonatomic, copy) NSString *name;

Member

@property (nonatomic, assign) BOOL Isvip;

Micro-Blog Content

@property (nonatomic, copy) NSString *text;

Image matching

@property (nonatomic, copy) NSString *picture;

+ (Instancetype) statuswithdict: (Nsdictionary *) dict;

-(Instancetype) Initwithdict: (Nsdictionary *) dict;

@end

2> implementation

#import "CZStatus.h"

@implementation Czstatus

+ (Instancetype) statuswithdict: (Nsdictionary *) dict {

return [[Self alloc] initwithdict:dict];

}

-(Instancetype) Initwithdict: (Nsdictionary *) dict

{

self = [super init];

if (self) {

[Self setvaluesforkeyswithdictionary:dict];

}

return self;

}

@end

2) The model of the frame of each row of cells

1> statement

#import <UIKit/UIKit.h>

@class Czstatus;

User name Body size

#define Namelabelfont [Uifont systemfontofsize:14.0]

Micro-bo text font size

#define Textlabelfont [Uifont systemfontofsize:15.0]

@interface Czstatusframe:nsobject

Data model

@property (nonatomic, strong) Czstatus *status;

/**

* Avatar Frame

*/

@property (nonatomic, assign, ReadOnly) CGRect iconf;

/**

* Nickname Frame

*/

@property (nonatomic, assign, ReadOnly) CGRect NAMEF;

/**

* VIP icon Frame

*/

@property (nonatomic, assign, ReadOnly) CGRect vipf;

/**

* Body Frame

*/

@property (nonatomic, assign, ReadOnly) CGRect TEXTF;

/**

* Frame with picture

*/

@property (nonatomic, assign, ReadOnly) CGRect PICF;

/**

* Row height of cell

*/

@property (nonatomic, assign, ReadOnly) CGFloat cellheight;

@end

2> implementation

#import "CZStatusFrame.h"

#import "CZStatus.h"

#define Spacing 8

@implementation Czstatusframe

Overrides the set method of the data model, in which the frame and row height of the child control is calculated

-(void) SetStatus: (Czstatus *) Status {

#warning mark-Override the Set method do not forget to assign values to the member variables of the downline line

_status = status;

Avatar

CGFloat IconX = Spacing;

CGFloat icony = Spacing;

CGFloat iconw = 35;

CGFloat iconh = iconw;

_iconf = CGRectMake (IconX, Icony, Iconw, Iconh);

Nickname

CGFloat namelx = Cgrectgetmaxx (_iconf) + Spacing;

CGFloat NameLY = Icony + Spacing;

Cgsize namesize = [self sizewithtext:_status.name font:namelabelfont maxsize:cgsizemake (MAXFLOAT, MAXFLOAT)];

_NAMEF = (cgrect) {{namelx, NameLY}, namesize};

VIP icon

if (_STATUS.ISVIP) {

CGFloat vipx = Cgrectgetmaxx (_NAMEF) + Spacing;

CGFloat vipy = NameLY + Spacing * 0.3;

CGFloat VIPW = 15;

CGFloat viph = 15;

_VIPF = CGRectMake (vipx, vipy, VIPW, VIPH);

}

Body

CGFloat textx = IconX;

CGFloat texty = Cgrectgetmaxy (_iconf) + Spacing;

CGFloat textmaxwidth = [UIScreen mainscreen].bounds.size.width-2 *spacing;

Cgsize textSize = [self sizewithtext:_status.text font:textlabelfont maxsize:cgsizemake (Textmaxwidth, MAXFLOAT)];

_TEXTF = (cgrect) {{textx, texty}, textSize};

Image matching

if (_status.picture) {

CGFloat picx = IconX;

CGFloat picy = Cgrectgetmaxy (_TEXTF) + Spacing;

CGFloat PICW = 100;

CGFloat PicH = PICW;

_PICF = CGRectMake (Picx, Picy, PICW, PicH);

}

_cellheight = MAX (Cgrectgetmaxy (_PICF), Cgrectgetmaxy (_TEXTF)) + Spacing;

}

/**

* Calculate Text Size

*

* @param text that needs to be computed

* @param font text fonts

* @param maxSize text Rectangle maximum Size

*/

-(Cgsize) Sizewithtext: (NSString *) text font: (Uifont *) font maxSize: (cgsize) MaxSize {

Nsdictionary *attributes = @{nsfontattributename:font};

return [text Boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attributes Context:nil].size;

}

@end

2.v--view is the view

Cell view

1) Disclaimer

#import <UIKit/UIKit.h>

@class Czstatusframe;

@interface Czstatuscell:uitableviewcell

Frame model

@property (nonatomic, strong) Czstatusframe *statusframe;

+ (Instancetype) Cellwithtableview: (UITableView *) TableView;

@end

2) Realization

#import "CZStatusCell.h"

#import "CZStatus.h"

#import "CZStatusFrame.h"

#define Margins 8

@interface Czstatuscell ()

@property (nonatomic, strong) Czstatus *status;

@property (nonatomic, weak) Uiimageview *iconview;

@property (nonatomic, weak) UILabel *nameview;

@property (nonatomic, weak) Uiimageview *vipview;

@property (nonatomic, weak) UILabel *textview;

@property (nonatomic, weak) Uiimageview *pictureview;

@end

@implementation Czstatuscell

Quickly create a cell

+ (Instancetype) Cellwithtableview: (UITableView *) TableView {

static NSString *id = @ "Cell";

Czstatuscell *cell = [TableView dequeuereusablecellwithidentifier:id];

if (cell = = nil) {

cell = [[Czstatuscell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id];

}

return cell;

}

Rewrite the Initwithstyle: method so that when the cell is created, there are already many child controls

-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier

{

self = [super Initwithstyle:style reuseidentifier:reuseidentifier];

if (self) {

Add the inner child control of a cell

[Self addstatuscellwithsubview];

}

return self;

}

Add the inner child control of a cell

-(void) Addstatuscellwithsubview {

Avatar

Self.iconview = [self createimageview];

Nickname

Self.nameview = [self createlabelwithfont:namelabelfont];

VIP icon

Self.vipview = [self createimageview];

Self.vipView.image = [UIImage imagenamed:@ "VIP"];

Body

Self.textview = [self createlabelwithfont:textlabelfont];

Image matching

Self.pictureview = [self createimageview];

}

Quickly create and add a label

-(UILabel *) Createlabelwithfont: (Uifont *) Font {

UILabel *label = [[UILabel alloc] init];

Label.numberoflines = 0;

Label.font = font;

[Self.contentview Addsubview:label];

return label;

}

Quickly create and add in ImageView

-(Uiimageview *) Createimageview {

Uiimageview *imageview = [[Uiimageview alloc] init];

[Self.contentview Addsubview:imageview];

return imageView;

}

Override the set method of the frame model, where you set the cell child control's data and frame

-(void) Setstatusframe: (Czstatusframe *) Statusframe {

_statusframe = Statusframe;

_status = Statusframe.status;

1. Setting up the data

[Self settingdata];

2. Calculate frame

[Self settingframe];

}

Setting Control Data

-(void) Settingdata {

Set up your avatar

Self.iconView.image = [UIImage Imagenamed:_status.icon];

Set User name

Self.nameView.text = _status.name;

Set user name color, member is red, normal user is black

Self.nameView.textColor = _status.isvip? [Uicolor Redcolor]: [Uicolor blackcolor];

Whether to show members

Self.vipView.hidden = _status.isvip? No:yes;

Set up your Weibo content

Self.textView.text = _status.text;

Whether to display a map

Self.pictureView.hidden = _status.picture? No:yes;

Set up a map

Self.pictureView.image = _status.picture? [UIImage Imagenamed:_status.picture]: nil;

}

Set the control frame

-(void) Settingframe {

Set User avatar Frame

Self.iconView.frame = Self.statusFrame.iconF;

Set User name Frame

Self.nameView.frame = Self.statusFrame.nameF;

Set the VIP frame

Self.vipView.frame = _status.isvip? Self.statusFrame.vipF:CGRectZero;

Set up a frame for Weibo content

Self.textView.frame = Self.statusFrame.textF;

Set frame with map

Self.pictureView.frame = _status.picture? Self.statusFrame.picF:CGRectZero;

}

@end

3.c--controller is the control layer

1) Disclaimer

#import <UIKit/UIKit.h>

@interface Czviewcontroller:uitableviewcontroller//inherit from Uitableviewcontroller here

@end

2) Realization

#import "CZViewController.h"

#import "CZStatus.h"

#import "CZStatusCell.h"

#import "CZStatusFrame.h"

@interface Czviewcontroller ()

@property (nonatomic, strong) Nsarray *statusframe;

@end

@implementation Czviewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Estimated Row Height

Self.tableView.estimatedRowHeight = 300;

}

Hide the status bar

-(BOOL) Prefersstatusbarhidden {

return YES;

}

Data source methods for #pragma mark-tableview

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {

return self.statusFrame.count;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {

Create cell

Czstatuscell *cell = [Czstatuscell Cellwithtableview:tableview];

Get the corresponding frame model for each line

Czstatusframe *statusframe = Self.statusframe[indexpath.row];

Assigning values to the cell's frame model

Cell.statusframe = Statusframe;

return cell;

}

#pragma mark-proxy method used to return row heights

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {

Returns the height of the cell

return [Self.statusframe[indexpath.row] cellheight];

}

Lazy Load Dictionary to model

-(Nsarray *) Statusframe {

if (_statusframe = = nil) {

Nsarray *dictarr = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Statuses.plist" OfType: Nil]];

Nsmutablearray *ARRM = [Nsmutablearray array];

[Dictarr enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {

Czstatusframe *statusframe = [[Czstatusframe alloc] init];

Czstatus *status = [Czstatus statuswithdict:obj];

Statusframe.status = status;

[Arrm Addobject:statusframe];

}];

_statusframe = ARRM;

}

return _statusframe;

}

@end

UITableView (simple construction of Weibo interface)

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.