Iphone开发代码片段1

http://www.cnblogs.com/likwo/archive/2010/08/03/1791461.html

 Iphone代码片段导航

 

 Iphone开发代码片段1

Iphone开发代码片段2

 Iphone开发代码片段3 

 

 

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;

}
从网上下载图片
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,如表格的cell的所有view找倒textfield

- (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];

} 

 

 程序登录和退出时,如果要做自动登录,那么就有必要保存用户的登陆信息,可放在NSUserDefault里,如何使用它,下面有个Sample

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"];

 

如何自动获取tableView每行的高度。

因为TableView的高度计算是先于TableCell的生成。所以必须先计算。参考网址http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

复制代码
代码
#define  FONT_SIZE 14.0f
#define  CELL_CONTENT_WIDTH 320.0f
#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 allocinit];

[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(00, width, 45)];

[toolbar setBarStyleUIBarStyleDefault];

// create an array for the buttons

NSMutableArray* buttons = [[NSMutableArray allocinitWithCapacity:1];

UIBarButtonItem *backButton = [[UIBarButtonItem allocinitWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:selfaction:@selector(backPress:)];

UIBarButtonItem *flexItem = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

  target:nil

  action:nil];

UIBarButtonItem *doneButton = [[UIBarButtonItem allocinitWithTitle:@"确定" style:UIBarButtonItemStyleBordered target:selfaction:@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]; 

 推迟某个方法的执行

[self performSelector:@selector(loadDataFromNet) withObject:nil afterDelay:0.1]; 

 

通过Animate的方式移动View

         
        [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];

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值