Iphone開發代碼片段1

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



如何自動獲取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 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];


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章