IPhone development code snippet 1

Source: Internet
Author: User
Tags dateformat

Uitableview uitextfield

Nsstring * text = (uitextfield *) cell. accessoryview). text;

However, you must be careful about setting up cells and accessing their values. if any cell goes offscreen, it will be removed and you will not be able to access the text field. what you want to do when setting up your cell is:

Cell. accessoryview = nil; // make sure any old accessory view isn' t there.

If (/* cell needs text field */){

Uitextfield * textfield = [[uitextfield alloc] initwithframe: frame] autorelease];

Textfield. Text = savedvalue;

Cell. accessoryview = textfield;

[Textfield addtarget: Self action: @ selector (textchanged :) forcontrolevents: uicontroleventvaluechanged];

}

...

-(Void) textchanged :( uitextfield *) Source {

Self. savedvalue = source. text;

}

Download images from the Internet

Id Path = @ "http://merrimusings.mu.nu/archives/images/groundhog2.jpg ";
Nsurl * url = [nsurl urlwithstring: path];
Nsdata * Data = [nsdata datawithcontentsofurl: url];
Uiimage * IMG = [[uiimage alloc] initwithdata: data cache: No];

Nsurl * myurl = [[nsurl alloc] initwithstring: @ "http://www.google.com/intl/en_ALL/images/logo.gif"];

Uiimage * myimage = [uiimage imagewithdata: [nsdata datawithcontentsofurl: myurl];

Cgsize imagesize = myimage. size;

Uiimageview * myimageview = [[uiimageview alloc] initwithframe: cgrectmake (320-imagesize.width)/2), (480-imagesize.height)/2), imagesize. Width, imagesize. Height)];

Myimageview. Image = myimage;

[Self. View addsubview: myimageview];

[Myurl release];
[Myimageview release];

[Myimage release];

Subviews of the sample control, such as finding the inverted textfield for all views of the cell in the table

-(Void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {

[Tableview deselectrowatindexpath: indexpath animated: Yes];

Uitableviewcell * cell = [tableview cellforrowatindexpath: indexpath];

Uitextfield * textfield = nil;

For (uiview * subview in cell. subviews)

{

If ([subview ismemberofclass: [uitextfield class])

{

Textfield = (uitextfield *) subview;

[Textfield becomefirstresponder];

}

}

// [Tableview deselectrowatindexpath: indexpath animated: Yes];

}

When the program logs on and exits, if you want to perform automatic logon, it is necessary to save the user's login information, which can be placed in nsuserdefault. How to use it, there is a sample below
How to Use nsuserdefault to save and load data
Saving

Nsuserdefaults * prefs = [nsuserdefaults standarduserdefaults];

// Saving an nsstring
[Prefs setobject: @ "texttosave" forkey: @ "keytolookupstring"];

// Saving an nsinteger
[Prefs setinteger: 42 forkey: @ "integerkey"];

// Saving a double
[Prefs setdouble: 3.1415 forkey: @ "doublekey"];

// Saving a float
[Prefs setfloat: 1.2345678 forkey: @ "floatkey"];

// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[Prefs synchronize];
Retrieving

Nsuserdefaults * prefs = [nsuserdefaults standarduserdefaults];

// Getting an nsstring
Nsstring * mystring = [prefs stringforkey: @ "keytolookupstring"];

// Getting an nsinteger
Nsinteger Myint = [prefs integerforkey: @ "integerkey"];

// Getting an float

Float myfloat = [prefs floatforkey: @ "floatkey"];

How to automatically obtain the height of each row in tableview.

Because the height of tableview is generated before tablecell. Therefore, it must be calculated first. Reference Web site http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
Code
# Define font_size 14.0f
# Define cell_content_width 3200000f
# Define cell_content_margin 10.0f

-(Cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath;
{
Nsstring * text = [items objectatindex: [indexpath row];

Cgsize constraint = cgsizemake (cell_content_width-(cell_content_margin * 2), 20000.0f );

Cgsize size = [text sizewithfont: [uifont systemfontofsize: font_size] constrainedtosize: constraint linebreakmode: uilinebreakmodewordwrap];

Cgfloat Height = max (size. Height, 44.0f );

Return height + (cell_content_margin * 2 );
}

-(Uitableviewcell *) tableview :( uitableview *) TV cellforrowatindexpath :( nsindexpath *) indexpath
{
Uitableviewcell * cell;
Uilabel * label = nil;

Cell = [TV dequeuereusablecellwithidentifier: @ "cell"];
If (cell = nil)
{
Cell = [[[uitableviewcell alloc] initwithframe: cgrectzero reuseidentifier: @ "cell"] autorelease];

Label = [[uilabel alloc] initwithframe: cgrectzero];
[Label setlinebreakmode: uilinebreakmodewordwrap];
[Label setminimumfontsize: font_size];
[Label setnumberoflines: 0];
[Label setfont: [uifont systemfontofsize: font_size];
[Label settag: 1];

[[Label layer] setborderwidth: 2.0f];

[[Cell contentview] addsubview: Label];

}
Nsstring * text = [items objectatindex: [indexpath row];

Cgsize constraint = cgsizemake (cell_content_width-(cell_content_margin * 2), 20000.0f );

Cgsize size = [text sizewithfont: [uifont systemfontofsize: font_size] constrainedtosize: constraint linebreakmode: uilinebreakmodewordwrap];

If (! Label)
Label = (uilabel *) [Cell viewwithtag: 1];

[Label settext: text];
[Label setframe: cgrectmake (cell_content_margin, cell_content_margin, cell_content_width-(cell_content_margin * 2), max (size. Height, 44.0f)];

Return cell;
}

Sndate reference

Http://iphonedevelopertips.com/cocoa/date-formatter-examples.html

Http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/df100103.html

Http://www.cocoachina.com/bbs/simple? T10151.html

Http://www.iphonedevsdk.com/forum/iphone-sdk-development/4528-help-nsdateformatter.html

Eee Mmm D hh: mm: ss z yyyy"

Tue APR 06 00:00:00 + 0800 2010

Nsstring * createtime = @ "Tue APR 06 00:00:00 + 0800 2010 ";

Nsdateformatter * dateformat = [[nsdateformatter alloc] init];

[Dateformat setdateformat: @ "eee Mmm D hh: mm: ss z yyyy"];

Nsdate * createdate = [dateformat datefromstring: createtime];

Uitoolbar Add button and add space between two uibuttonitem

Uitoolbar * toolbar = [uitoolbar alloc]

Initwithframe: cgrectmake (0, 0, width, 45)];

[Toolbar setbarstyle: uibarstyledefault];

// Create an array for the buttons

Nsmutablearray * buttons = [[nsmutablearray alloc] initwithcapacity: 1];

Uibarbuttonitem * backbutton = [[uibarbuttonitem alloc] initwithtitle: @ "return" style: uibarbuttonitemstylebordered target: Self action: @ selector (backpress :)];

Uibarbuttonitem * flexitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemflexiblespace

Target: Nil

Action: Nil];

Uibarbuttonitem * donebutton = [[uibarbuttonitem alloc] initwithtitle: @ "OK" style: uibarbuttonitemstylebordered target: Self action: @ selector (donepress :)];

[Buttons addobject: backbutton];

[Buttons addobject: flexitem];

[Flexitem release];

[Buttons addobject: donebutton];

[Backbutton release];

[Donebutton release];

[Toolbar setitems: buttons animated: No];

[Self. View addsubview: toolbar];

[Toolbar release];

Delay the execution of a Method

[Self defined mselector: @ selector (loaddatafromnet) withobject: Nil afterdelay: 0.1];


Move the view in the animate Mode

[Uiview beginanimations: Nil context: NULL];
[Uiview setanimationduration: kanimationdurationstart];
[Uiview setanimationcurve: uiviewanimationcurveeaseinout];
[View setframe: cgrectmake (0, [uiapplication sharedapplication]. statusbarframe. Size. Height, view. Frame. Size. Width, view. Frame. Size. Height)];
[Uiview commitanimations];

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.