Iphone代码片段导航
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 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 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:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(backPress:)];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" 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];
推迟某个方法的执行
[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];