[UITableView] 테이블 셀(UITableViewCell) 선택시 배경색 변경하기

iPhone Development 2011/03/19 22:35

테이블뷰에서 특정 셀을 선택하고 있을 때에(Highlighted) 배경색을 바꿔 보겠습니다.

기본적으로 테이블에서 셀을 선택하면 아래와 같이 파란색배경으로 변경이 된다.



기본적으로 UITableViewCell의 selectionStyle 프로퍼티를 이용하여 파란색, 회색, 없음을 선택할 수 있습니다.

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; // 파란색
[cell setSelectionStyle:UITableViewCellSelectionStyleGray]; // 회색
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; // 없음


음..그런데 뭔가 아쉽습니다. 다른색도 있었으면 좋을텐데 말이죠.
그래서 UITableViewCell에서는 Highlighted상태일 때에 setHighlighted:animation: 메소드가 호출됩니다.

* UITableViewCell Class Reference 참고

UITableViewCell을 상속받는 CustomTableViewCell을 만들어 setHighlighted:animation: 메소드를 오버라이딩하는 방법으로 배경색을 변경해 보도록 하겠습니다.

* 배경
- Xcode 4, iOS SDK 4.3
- 네비게이션



Step 1: 먼저 UITableViewCell을 상속받은 CustomTableViewCell을 생성합니다.

Step 2: setHighlighted:animation: 메소드 오버라이드

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //배경이 보일 수 있게 배경위의 View들을 투명하게 처리
        [self.contentView setBackgroundColor:[UIColor clearColor]];
        [self.textLabel setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
   
    // Highlighted - 빨간색
    // Normal - 투명
    [self setBackgroundColor:highlighted ? [UIColor redColor] : [UIColor clearColor]];
    [self.textLabel setTextColor:highlighted ? [UIColor whiteColor] : [UIColor blackColor]];
}


적당한 위치에 위의 함수를 넣습니다.


Step 3: UITableView가 CustomTableViewCell을 사용하게 수정


//- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
- (void)configureCell:(CustomTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[managedObject valueForKey:@"timeStamp"] description];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    /* 기존 코드
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
     */
   
   
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    // Configure the cell.
    [self configureCell:cell atIndexPath:indexPath];
    return (UITableViewCell *)cell;
}


Step 4: 확인

이제 빌드하고 실행하면 아래와 같이 빨간색이 나타나는 것을 확인할 수 있습니다.










* 예제소스 다운로드








 

Trackback 0 : Comment 1
  1. 힘찬아빠 2011/10/28 18:49 Modify/Delete Reply

    Step 1: 먼저 UITableViewCell을 상속받은 CustomTableViewCell을 생성합니다.
    ... 에 대한 설명은 없으시네요;;;

Write a comment


오픈소스 RDP iRdesktop 빌드하기

iPhone Development 2011/03/09 00:10

자주가는 맥부기까페에 어느 회원분이 iRdesktop을 빌드하려는데 잘 되지 않는 다는 글을 보고 한번 해보았습니다.


도움이 되길 바랍니다.


iRdesktop은 iPhone/iPad용 오픈소스 RDP입니다. XCode(iOS SDK 4.2)에서 한번 빌드해 보겠습니다.


Step 1: iRdesktop소스코드를 다운로드 합니다.

http://www.irdesktop.com/gpl/iRdesktop-src-1.0.5.zip

사파리에서 받으면 일반적으로 /Users/사용자이름/Downloads/iRdesktop-src-1.0.5 에 다운로드됩니다. 압축도 풀어지구요.


Step 2: OpenSSL 다운로드 및 빌드

성격 급하신 분들은 다운 받자마자 XCode를 열어 빌드부터 하시죠?;; 

그리고는 엄청난 빌드에러에 좌절을...(네.. 제가 그랬습니다.)


이 빌드 오류는 OpenSSL라이브러리를 찾지 못해서 발생하는 에러입니다.


그래서 iRdesktop소스에 어떤 파일이 있나 찾아보니 

OpenSSL-DownloadAndBuild.command 와 OpenSSL-Thinstuff.diff 라는 파일이 있습니다. 

OpenSSL을 다운로드하고 빌드하는 실행 파일과 OpenSSL소스 패치 파일이네요. 

(또 성격 급하신 분들은 더블클릭으로 실행합니다. 그렇게 하면 /Users/사용자이름/에 파일이 받아지고 빌드가 되어 빌드오류가 납니다. 네..또 접니다;;)


터미널을 열어 OpenSSL-DownloadAndBuild.command파일이 있는 곳으로 이동합니다.

------------------------------------------------------------------------------------------------------------------

$ pwd

/Users/juncon

$ cd Downloads/iRdesktop-src-1.0.5/

------------------------------------------------------------------------------------------------------------------




이제 OpenSSL-DownloadAndBuild.command 파일을 실행합니다.

------------------------------------------------------------------------------------------------------------------

$ ./OpenSSL-DownloadAndBuild.command 

Downloading OpenSSL Version 0.9.8j ...

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100 3650k  100 3650k    0     0  49823      0  0:01:15  0:01:15 --:--:--  108k

Unpacking OpenSSL ...


Applying Thinstuff patch ...

patching file Configure

patching file crypto/ui/ui_openssl.c


Copying header hiles ...


Building i386 version (for simulator). Please wait ...

Done. Build log saved in BuildLog.darwin-i386.txt


Building armv6 version (for iPhone). Please wait ...

Done. Build log saved in BuildLog.darwin-armv6.txt

cp: libcrypto.a: No such file or directory

cp: libssl.a: No such file or directory


Finished. Please verify the contens of the openssl folder in your main project folder

------------------------------------------------------------------------------------------------------------------


어랏..끝난 것 같긴 한데... 아래 문구가 눈에 거슬리네요.

cp: libcrypto.a: No such file or directory

cp: libssl.a: No such file or directory


결국 시뮬레이터용은 빌드가 성공적으로 되었는데 arm6용(디바이스)은 실패한 것 같습니다.ㅠㅠ 


음.. 빌드로그를 보니 iOS SDK가 4.1기준으로 되어 있네요. 저는 참고로 SDK 4.2가 설치되어 있습니다.

그래서 어딘가에 iOS SDK 4.1을 설정해주는 부분이 있을 것 같아 여기저기 뒤져보니

OpenSSL-Thinstuff.diff 파일에 /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.1.sdk 라는 문구가 있네요. 

그냥 맨 마지막 iPhoneOS4.1.sdk를 iPhoneOS4.2.sdk로 수정하고 다시 빌드해보겠습니다.


------------------------------------------------------------------------------------------------------------------

$ ./OpenSSL-DownloadAndBuild.command 

Unpacking OpenSSL ...


Applying Thinstuff patch ...

patching file Configure

patching file crypto/ui/ui_openssl.c


Copying header hiles ...


Building i386 version (for simulator). Please wait ...

Done. Build log saved in BuildLog.darwin-i386.txt


Building armv6 version (for iPhone). Please wait ...

Done. Build log saved in BuildLog.darwin-armv6.txt


Finished. Please verify the contens of the openssl folder in your main project folder

------------------------------------------------------------------------------------------------------------------


음...이번엔 뭔가 깔끔하게 끝난 것 같군요. XCode로 프로젝트를 빌드해 봐야겠습니다.


Step 3: 프로젝트 빌드

파인더에서 iRdesktop.xcodeproj를 열어 Base SDK를 iOS 4.2로 수정하고 빌드를 합니다.


음..다음과 같은 오류가 1개 있네요;;




앗.. 처음보는 에러메시지;; 무슨 말인지는 모르겠으나..버전이 어쩌구 저쩌구 하네요. 

급한 마음에 일단 빨간 줄을 더블클릭하니 인터페이스 빌더가 실행됩니다.



위에 빨간 네모 부분에 있는 오류아이콘을 눌러보니 아래와 같이 나옵니다.







음... iOS 2.0으로 되어 있는게 맘에 들지 않네요. 지금 4.3에 iOS 5도 나온다고 하는데 말이죠. 

그냥 Deployment Target:을 iOS 3.0으로 변경 해봅니다.









어랏! 경고1개와 오류1개가 사라졌네요. (왜? 냐고는 묻지 말아주세요 -_-) 일단 잘 되니 넘어가겠습니다. 저장하고 다시 빌드합니다.



Step 4: 실행


잘 되는군요^^  고생하셨습니다.





끝~

Trackback 0 : Comments 2
  1. cloim 2011/03/09 10:39 Modify/Delete Reply

    그 어느 회원분이 접니다 ㅎㅎ 너무 감사해요! 덕분에 잘 빌드했습니다. 글 참고해서 제 블로그에도 정리해 두었는데 괜찮으시죠? ; 앞으로도 많이 가르쳐 주세요!!

    • 전수근 juncon 2011/03/09 11:26 Modify/Delete

      앗 반갑습니다.ㅎㅎ 제 게시물은 어디로 가져가셔도 문제 없습니다^^
      도움이 되었다니 다행이네요^^

Write a comment


[팁] 슬립모드에 관하여 (The Automatic Sleep Timer)

iPhone Development 2010/11/06 10:23
아이폰을 실행해 두고 일정시간동안 터치 이벤트가 없으면 배터리를 절약하기 위하여 화면이 꺼지고 실행중인 어플리케이션은 백그라운드 상태로 들어가게 됩니다.

그러나 사용자의 터치 이벤트가 없어도 계속 켜져있어야 하는 경우가 있을 수 있습니다. 

간단히 아래의 코드로 Sleep Timer를 켜고 끌 수 있습니다.

- Sleep Timer Disable

[UIApplication sharedApplication].idleTimerDisabled = YES;


- Sleep Timer Enable

[UIApplication sharedApplication].idleTimerDisabled = NO;



idleTimerDisabled의 기본값은 1(NO)입니다.  
Trackback 0 : Comment 0

Write a comment

◀ PREV : [1] : [2] : [3] : [4] : [5] : ... [16] : NEXT ▶