XCODE2010.11.07 01:04
[아이폰 앱 개발] customcell사용시 메모리효율적이용, 즉 재사용시키려면 해야할 것.

The UITableView class is a wonder of efficient memory management, if you use it correctly.

Here’s the standard template code that Xcode generates when you create a subclass of UITableViewController:

  1. // Customize the appearance of table view cells.  
  2. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  4.     static NSString *CellIdentifier = @"Cell";  
  6.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  7.     if (cell == nil) {  
  8.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  9.     }  
  11.     // Set up the cell...  
  13.     return cell;  
  14. }  

The keys here are the CellIdentifier variable and the call to dequeueReusableCellWithIdentifier, which enable the iPhone OS to reuse existing instances of UITableViewCell whenever possible.

(Don’t create a unique reuse identifier for each row as I’ve seen some developers do. Yes, it’s much easier to deal with asynchronous download of images for each row if you know how to uniquely identify the cell, and you know that the cell is still in memory. But that totally defeats the efficient memory management that UITableView is capable of.)

Under normal circumstances a UITableView will create one instance of a UITableViewCell per row that is visible on the screen. As you scroll, the cell instance that just rolled off the screen will be reused for the cell that is about to appear.

To verify that this memory management is working as it should, add a log statement each time a new cell is created:

  1. if (cell == nil) {  
  2.     DLog(@"creating a new cell");  
  3.     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  4. }  

When you run your app and start scrolling your table view, you should not see any creation of cells beyond the initial list (plus one). If you see “creating a new cell” log statements scrolling off the screen as you scroll the table view, you’ve got a problem.

If you just follow the standard Xcode template above, you should be fine. However if you’re loading a Nib for a custom table view cell layout using Apple’s recommended way, there’s an important detail you must not forget. (Tip of the hat to Jeff LaMarche for inspiring this blog post.)

Here’s the typical NIB loading code from Apple:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.     static NSString *CellIdentifier = @"CheckedTableViewCell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  5.     if (cell == nil) {  
  6.         DLog(@"creating a new cell");  
  8.         // Load the table view cell from a Nib file.  
  9.         [[NSBundle mainBundle] loadNibNamed:@"CheckedTableViewCell" owner:self options:nil];  
  11.         // The checkedTableViewCell property is just a temporary placeholder for loading the Nib.  
  12.         cell = checkedTableViewCell;  
  14.         // We don't need this anymore, so set to nil.  
  15.         self.checkedTableViewCell = nil;  
  16.     }  
  18.     return cell;  
  19. }  

The key here is that the CellIdentifier value must also be entered into Interface Builder, like this:

If you don’t do this, then UITableViewCells will not be reused. (A telltale sign of this is that you’ll see lots of “creating a new cell” log messages.) There is no compiler or runtime warning if you fail to enter this critical piece of information into Interface Builder. So that log statement can be a useful warning.

Posted by 오늘마감
XCODE2010.11.06 21:11
[아이폰 앱 개발] cancel alert

// Show an alert if "Appleseed" is not in Contacts

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 

message:@"Could not find Appleseed in the Contacts application" 

[alert show];

[alert release];

Posted by 오늘마감
XCODE2010.11.06 17:27
[아이폰 앱 개발] UIBarButtonItem을 viewDidLoad에서 만들고 숨기기


UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];

temporaryBarButtonItem.title = @"Back";

self.navigationItem.backBarButtonItem = temporaryBarButtonItem;

[temporaryBarButtonItem release];



현재뷰에서 다음뷰갈때 위에서 만든 barButton숨기기

- (void)showDetails:(id)sender


    // the detail view does not want a toolbar so hide it

    [self.navigationController setToolbarHidden:YES animated:NO];


    [self.navigationController pushViewController:self.detailViewController animated:YES];


Posted by 오늘마감
XCODE2010.11.06 12:30
[아이폰 앱 개발] 뷰로딩시 이전페이지에서 넘어온 ToolBar숨기기

- (void)viewDidAppear:(BOOL)animated


    // bring back the toolbar

    [self.navigationController setToolbarHidden:NO animated:NO];


Posted by 오늘마감
XCODE2010.11.06 08:06
[아이폰 앱 개발] 객체의 클래스타입 일치여부 체크

Car myCar =[[Car alloc]init]; 


if ([myCar isKindOfClass:[Car class]]){




Posted by 오늘마감
XCODE2010.11.06 04:21
[아이폰 앱 개발] 위치정보요청 CLLocationManager
헤더에 우선 CLLocationManagerDelegate를 추가해야하고, CoreLocation.frameWork추가

//위치정보얻는데 보통 바로 얻으나, 지역에 따라 느리면, 수초걸릴 수도있다.

CLLocationManager *locationManager=[[CLLocationManager alloc]init];
//정밀도지정 // 정밀도높일 수록 전력소모 증가
//return값 double, 10미터오차범위 현재위치결정

//위치바뀔때마다 locationManager는 델리게이트에통보
//보통 필터세팅된 거리이탈시 통보// 1000미터범위 이탈시 통보세팅

//필터해제시 locationManager.distanceFilte=KCLDistanceFilterNone;

[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];

//Error noti

-(void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error{
NSSString *errType=(error.code==KCLerrorDenied)?@"Access Denied":@"UnKnown Error";

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error getting Location"

[alert show];
[alert release];


//이동거리측정(최신버젼에서 depricated된듯한데 확인필요)

CLLocationDistance distance =[newLocation getdistanceFrom:startingPoint];
NSString *distancesString=[[NSString alloc]initWithFormat:@"%gm",distance];
[distanceString release];

Posted by 오늘마감
XCODE2010.11.06 01:04
[아이폰 앱 개발] NSString split하기
<form name="search" action="http://www.idev101.com/search.html" method="POST">

Split a String into an Array


Split a string into an array of strings by using NSString's componentsSeparatedByString:

NSString *myString = @"This is a test"; NSArray *myWords = [myString componentsSeparatedByString:@" "]; // myWords is now: [@"This", @"is", @"a", @"test"]

If you need to split on a set of several different characters, use NSString's componentsSeparatedByCharactersInSet:

NSString *myString = @"Foo-bar/blee"; NSArray *myWords = [myString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"-/"] ]; // myWords is now: [@"Foo", @"bar", @"blee"]

The separator string can't be blank. If you need to separate a string into its individual characters, just loop through the length of the string and convert each char into a new string:

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]]; for (int i=0; i < [myString length]; i++) { NSString *ichar = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]]; [characters addObject:ichar]; }

Posted by 오늘마감
[아이폰 앱 개발] NSString 스트링포맷 와일드카드써서 파일명바꾸기

int month;


NSString *imageName = [NSString stringWithFormat:@"2010_%d.png", month];


Posted by 오늘마감
[아이폰 앱 개발] Display Images

Display Images

Display an image anywhere on the screen, without using UI Builder. You can use this for other types of views as well.
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f); UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect]; [myImage setImage:[UIImage imageNamed:@"myImage.png"]]; myImage.opaque = YES; // explicitly opaque for performance [self.view addSubview:myImage]; [myImage release];
Posted by 오늘마감
[아이폰 앱 개발] Application Frame얻기

Application Frame
Use "bounds" instead of "applicationFrame" -- the latter will introduce a 20 pixel empty status bar (unless you want that..)
Posted by 오늘마감

티스토리 툴바