Objective-c--ui Basic Development Day eighth (QQ chat interface)

Source: Internet
Author: User
Tags notification center

First, the knowledge point:

QQ Chat Interface

Use of dual models (Datamodel and Framemodel)

Use of Uitextfield

Use of notifications

Two ways to stretch a picture (the Resizeableimagewithcapinsets property of a Slicing/image object)

Enumeration

Extraction of methods (same take out, different parts as parameters)

Second, set the basic format of TableView

1) define TableView basic

Numberofsectionsintableview: Setting Blocks

Numberofrowsinsection: Sets the corresponding number of rows per block

Cellforrowatindexpath: Setting the cell style

2) Cell Reuse

Set the cell

satic nsstring *identifier [email protected] "Qqcell";

Find from the cache pool first

Qqcell *cell =[_tableview Dequeuesablecellwithidentifier:identifier];

If the cell is not found in the cache pool, create

if (Cell==nil)

{cell =[[qqcell alloc]initwithstyle:uitableviewcellstyledefault reuserindentiier:identifier];}

3) Set the custom cell format (note why the message box is a button: Because you can set the text, you can set the picture)

A. Define the attributes and styles that need to be used

@property (nonatomic,weak) UILabel *timelabel;

@property (nonatomic,weak) UIButton *textbutton;

@property (nonatomic,weak) Uiimageview *iconimage;

b, define the initialization method, note the same method that is called when the cell is created in Viewcontroller

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

if (self =[super initwithstyle:style reuseidentifier:reuseidentifier]{

[Self setupui];//styles such as button, label internally

}}return self;

C, dual model (the data model is passed to the frame model in lazy loading: Reason:
1, lazy loading, the method only need to execute once

2, the line high RowHeight method must be called before the cell method call, so to implement the frame calculation in the cell, the order of delivery is problematic

Delivery mode: Defines the Qqmodel attribute in Framemodel

A. Define the Model property

@class Qqmodel;

@property (nonatomic, strong) Qqmodel *qqmodel;

B. Implement assignment in Lazy loading

Qqmodel *model =[qqmodel qqmodelwithdictionary:dict];

Qqframemodel *framemodel =[qqframemodel Alloc]init];

Framemodel.qqmodel =model;//Delivery Success

C, pass the value of Framemodel into the DataArray array, because this will be in the Framemodel (the data model also contains the various controls of the frame)

[_dataarray Addobject:framemodel];

D, how to set the frame according to the text data content

1), set the maximum adjustable area (if no limit: maxfloat)

Cgsize Maxarea =cgsizemake (textmaxa,maxfloat);

2), set the attribute dictionary (note that the setting of the font size of the property must match the font size in the original control, otherwise, it may have been in the text with an ellipsis)

Nsdictionary *dict [email Protected]{nsfontattributename:[uifont systemfontofsize:17};

3), set the real size of the object data boundingrectwithsize

Cgsize realsize =[_qqmodel.text Boundingrectwithsize:maxarea options:nsstringdrawinguseslinefragmentorigin Attributes:dict context:nil].size;

  

E, call the Framemodel and data model in the cell's settings class and set the

1) Override the Framemodel set method:

@property (Nonatomic,strong) Qqframemodel *framemodel;

-(void) Setframemodel: (Qqframemodel *) framemodel{

2) The model must be instantiated

_framemodel =framemodel;

3) Set the data in the model and frame

}

How to get Qqmodel

#import "QQModel.h"

Qqmodel *qqmodel =_framemodel.qqmodel;

F, code to achieve picture stretching

/**

1. Get Pictures

2. Set the center area of the stretched line (the smaller the area the better, so the width of the height is set to its centerline)

3. Call the Resizableimagewithcapinsets property of the Image object

*/

UIImage *image =[uiimage Imagenamed:imagename];

CGFloat *halfwidth =IMAGE.SIZE.WIDTH/2;

CGFloat *halfheight =IMAGE.SIZE.HEIGHT/2;

UIImage *resizeimage =[image resizableimagewithcapinsets:uiedgeinsetsmake (Halfheight,halfwidth,halfheight, Halfwidth) Resizingmode:uiimageresizingmodestretch];

G, note that after the picture is stretched, the text may not be inside the picture, you can increase the frame size of the button while setting the margin of the text (Contentedgeinsets uiedgeinsetsmake)

Textbutton.contentedgeinsets =uiedgeinsetsmake (20,20,20,20);

H, set line height

Heightforrowatindexpath

QQ Framemodel *framemodel =self.dataarray[indexpath.row];

Return Framemodel.cellheight

Three, Comparison time (22 comparison, the same time the label is displayed only once, you can know, as long as the comparison, but also to obtain the last time, preferably in lazy load implementation)

/**

0. Set a Hiddentimelabel property in Qqmodel to store the results of the comparison

1. Remove the last data

2, compare, if the same, go to qqmodel into the Hiddentimelabel=yes

*/

@property (Nonatomic,assign,getter = Ishiddentimelabel) BOOL Hiddentimelabel;

Qqframemodel *latframemodel =self.dataarray.lastobject;

if ([LastFrameModel.qqModel.time isEqualToString:frameModel.qqModel.time])

{

Qqmodel.hiddentimelabel=yes;

}

Call:

_timelabel.hidden =qqmodel.ishiddentimelabel; (You can call the overridden get method directly in the hidden property)

Iv. Supplemental Use enumeration (internal value category, enumerated value name, default starting from 0)

typedef ns_enum (Nsinteger,qqusertype)

{

qqusertypeother=0;

Qqusertypeme,

};

Benefits of enumerations: converting meaningless numbers into meaningful characters, increasing code readability

Five, set the bottom of the bullet box (learn TextField properties and notification concepts)

Five. Use of 1 TextField

[e-mail protected] "placeholder"

UIView *left =[[uiview alloc]initwithframe:cgrectmake (100, 0, 10, 10)];

Left.backgroundcolor=[uicolor Orangecolor];

Text.leftview=left;

Text.leftviewmode = Uitextfieldviewmodealways;

(You must set Uitextfieldviewmode when adding leftview, otherwise it cannot be displayed.)

Leftview/rightview/leftviewmode/rightviewmode

Password:
Text.securetextentry=yes;

Text.cleanbuttonmode =uitextfieldviewmodewhileediting;

V. 2) Use of notices

/**

1. Registered Listener

2, Notification Center release notice

3. Remove Listener from destruction

*/

1

/**

Addobserver: Listening Object

Selector: A method that is called after a listener has received a notification

Name: Names of notifications

Object: The publisher of the notification

*/

[[Nsnotificationcenter Defaultcenter] Addobserver:zhangsan selector: @selector (recievenotification:) name:@ "HuoYing [Object:souhuhuoying];

2

/**

The method to be executed after the notification is accepted

*/

-(void) Recievenotification: (nsnotification *) Noti

{

Nsdictionary *c = Noti.userinfo;

Company *company = c[@ "Company");

NSLog (@ "%@ subscribed to%@ Company's%@ video has been updated", self.personname,company.companyname,company.moviename);

NSLog (@ "%@", Noti);

}

3

-(void) dealloc

{

[[Nsnotificationcenter Defaultcenter]removeobserver:self.personname];

}

Five, 3) set the keyboard monitoring, by listening to pass the notification, change the bottom input frame frame and tableview frame

1,//Add monitoring, the keyboard will be hidden when, call

[[Nsnotificationcenter Defaultcenter] Addobserver:self

Selector: @selector (keyboardwilldisappear:)

Name:uikeyboardwillhidenotification

Object:nil];

The name of the listener notification that is called includes: uikeyboardwillhidenotification/keyboard notification name that is about to be hidden

name:uikeyboardwillchangeframenotification/the name of the notification that the keyboard hides or appears

uikeyboardwillshownotification/the name of the notification that appears on the keyboard

2. Animations to be performed when a notification message is received: (UIView animatewithduration)

[UIView animatewithduration:interval animations:^{

Self.view.transform = cgaffinetransformmaketranslation (0, (keyboardendy-keyboardbeginy));

}];

3, the user if scrolling tableview, or user input completed press RETURN, the keyboard needs to disappear

-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView {

Revoke TextField's first responder identity

[_textfield Resignfirstresponder];

}

4, by default, TableView should scroll to the last line (UITableView Scrolltorowatindexpath)

-(void) Scrolltobottom {

Nsindexpath *indexpath = [Nsindexpath indexpathforrow:self.dataarray.count-1 insection:0];

[_tableview Scrolltorowatindexpath:indexpath Atscrollposition:uitableviewscrollpositionbottom Animated:YES];

}

5, since it is a chat box, then the cell should not have been selected when the color (selectionstyle)

Do not change color after selecting cell

Cell.selectionstyle = Uitableviewcellselectionstylenone;

6. Withdrawal of monitoring

#warning must not forget to remove the listener.

-(void) Dealloc {

[[Nsnotificationcenter Defaultcenter] removeobserver:self];

}

Vi. Setting the time format

/**

1. Removal time (nsdate date)

2. Initialize format nsdateformatter (nasdateformatter) formatter.dateformat (attributes)

3. Date to character formatter stringfromdate:currentdate

*/

Take out the current time

NSDate *currentdate = [NSDate Date];

Format the time

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

Time format

YYYY-MM-DD HH:MM:SS Time Format

Formatter.dateformat = @ "hh:mm";

NSString *datestring = [Formatter stringfromdate:currentdate];

Vii. the difference between notice and agent

Common: All messages can be delivered

Difference: The agent is a one-to-many delivery, the notification is many to more, can have multiple message publishers and multiple message recipients

For example, the agent can know when the textfiled starts to enter a value, when to end the input value, but like the keyboard this pop-up time is not controllable, need to monitor this in real-time need to notify the center to publish the message.

Objective-c--ui Basic Development Day eighth (QQ chat interface)

Related Article

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.