아이폰어플개발정보2010. 10. 29. 10:29
[아이폰 앱 개발] 아이폰 어플 개발 Table View 의 기초

Table View Basics
iPhone tables(UITableView) 의 행(Row)수는 제한이 없음.(iPhone OS Memory)
UITableView - 테이블의 Data를 표시하는 뷰 오브젝트.
UITableViewCell - 테이블의 단일 행을 표현.

UITableViewCell 에 복수의 데이터를 추가하기 위해서
1.UITableViewCell 에 subview 를 추가 하거나
2.UITableViewCell 을 상속하는 서브클래스 작성.

Table > Section > Rows...
그룹 테이블(Figure 8-3)에서 각 Section 은 한 그룹을 나타냄.
인덱스 테이블(Figure 8-3)에서 각 Section 은 한 인덱스의 집합.

Implementing a Simple Table
Writing the Controller

  1. // delegate, datasource protocol 선언.  
  2. // datasource 는 테이블 각 행에 대한 데이터 핸들.  
  3. // delegate 는 데이터의 설정(font, user interation...) 핸들.  
  4. @interface SomeUITableView  {  
  5.     // Table Data.  
  6.     NSArray *tableData;  
  7.     ...  
  8. }  
  9.   
  10. #pragma mark -  
  11. #pragma mark Table View Data Source Methods  
  12. // 해당 Section 에 행 수.  
  13. - (NSInteger)tableView:(UITableView *)tableView  
  14.  numberOfRowsInSection:(NSInteger)section {  
  15.     return [self.tableData count];  
  16. }  
  17.   
  18. // 행이 그려질 때 호출되어 현재 표시될 행의 UITableViewCell 를 리턴.  
  19. // NSIndexPath 를 이용해서 해당 Section[indexPath section]과  
  20. // 해당 행[indexPath row]을 알 수 있음.  
  21. // UITableViewCell 는 캐쉬화 되고 시스템 메모리가 부족할 때 자동 메모리 해제됨.  
  22. - (UITableViewCell *)tableView:(UITableView *)tableView  
  23.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  24.     // cache 할 때 구분자.  
  25.     static NSString *SomeID = @"SomeID";  
  26.   
  27.     // cache 에 저장된 UITableViewCell 취득.  
  28.     UITableViewCell *cell = [tableView   
  29.                              dequeueReusableCellWithIdentifier:SomeID];  
  30.   
  31.     if (cell == nil) {  
  32.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero  
  33.                  reuseIdentifier:SomeID] autorelease];  
  34.     }  
  35.     ...  
  36. }  


Adding an Image
UITableViewCell 의 기본 옵션으로 각 행의 왼쪽에 이미지를 붙일 수 있음.
  1. // tableview:cellForRowAtIndexPath:  
  2. ...  
  3. UIImage *image = [UIImage imageNamed:@"someImage.png"];  
  4. cell.image = image;  
  5. ...  


Addtional Configurations
Setting the Indent Level
  1. #pragma mark -  
  2. #pragma mark Table View Delegate Methods  
  3. // 각 행의 indent level(NSInteger) 설정.  
  4. - (NSInteger)tableView:(UITableView *)tableView  
  5. indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {  
  6.     ...  
  7.     return indentLevel;  
  8. }  


Handling Row Selection
USER 가 선택한 행의 이벤트 전후로 핸들링이 가능.
  1. - (NSIndexPath *)tableView:(UITableView *)tableView  
  2.   willSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
  3.     // USER 선택에 대한 이벤트 전처리.  
  4.     ....  
  5. }  
  6.   
  7. - (NSIndexPath *)tableView:(UITableView *)tableView  
  8.    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
  9.     // USER 선택에 대한 이벤트 후처리.  
  10.     ....  
  11. }  


Changing Font Size and Row Height
  1. // tableview:cellForRowAtIndexPath:  
  2. ...  
  3. cell.font = [UIFont boldSystemFontOfSize:80];  
  4. ...  
  5. // 열의 높이 설정.  
  6. - (CGFloat)tableView:(UITableView *)tableView  
  7. heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
  8.     ....  
  9.     return height;  
  10. }  
Posted by 오늘마감

댓글을 달아 주세요

아이폰어플개발정보2010. 10. 12. 04:51
Creating a custom Table View Cell programmatically

Ohhk, quite a lot of things done but this one is something which I have used most often. Custom cells can sometimes greatly push ahead the usability of your application. In this post I am going to create a test project which will demonstrate how to create custom cells and use them appropriately to provide better usability. The application will finally look like this:


So to start open xcode and create a new project, chose the template as “Navigation Based” and name it as “CustomCellTestProject”. What template you chose does not matter, refer my previous posts to find how you can start working on any template.
First thing we will do is create a customCell. Right click on Classes and add a new UITableViewCell subclass. Name it as “CustomCell”. Now open CustomCell.h and add the following code:
#import

@interface CustomCell : UITableViewCell {

UILabel *primaryLabel;

UILabel *secondaryLabel;

UIImageView *myImageView;

}

@property(nonatomic,retain)UILabel *primaryLabel;

@property(nonatomic,retain)UILabel *secondaryLabel;

@property(nonatomic,retain)UIImageView *myImageView;

@end


We create the three elements and added them to the contentView of our cell.
synthesize all the three elements in CustomCell.m as we are going to access these elements from other classes.

@synthesize primaryLabel,secondaryLabel,myImageView;


Here we have simply added a primary label to display the primary text, a secondary label and an imageView. These elements will be created and added into the content view of our custom cell. So open CustomCell.m and add the following code

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {

if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

// Initialization code

primaryLabel = [[UILabelalloc]init];

primaryLabel.textAlignment = UITextAlignmentLeft;

primaryLabel.font = [UIFontsystemFontOfSize:14];

secondaryLabel = [[UILabelalloc]init];

secondaryLabel.textAlignment = UITextAlignmentLeft;

secondaryLabel.font = [UIFontsystemFontOfSize:8];

myImageView = [[UIImageView alloc]init];

[self.contentView addSubview:primaryLabel];

[self.contentView addSubview:secondaryLabel];

[self.contentView addSubview:myImageView];

}

returnself;

}



Now, we have already added the UI elements into our cell but you must have noticed, we have not yet defined how these elements will appear inside cell. Go ahead and add the following code for that:

- (void)layoutSubviews {

[superlayoutSubviews];

CGRect contentRect = self.contentView.bounds;

CGFloat boundsX = contentRect.origin.x;

CGRect frame;

frame= CGRectMake(boundsX+10 ,05050);

myImageView.frame = frame;

frame= CGRectMake(boundsX+70 ,520025);

primaryLabel.frame = frame;

frame= CGRectMake(boundsX+70 ,3010015);

secondaryLabel.frame = frame;

}


You can do anything in this method to define the lay out of cell. I have simply assigned frames to all the elements.
You can also find a method in CustomCell.m

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state

}

This method can be used to define how your cell should react when it is selected. You can describe what should be the highlight color or may be you want to flash one of the labels anything of your choice. I am leaving this method as it is.
We are done with creating our custom cell and now we have to use it. Open RootViewController.m and import CustomCell.h at the top.


#import “CustomCell.h”

I am going to create 5 cells here, you can just use your own logic of specifying number of cells and data. So change the following method to look like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 5;

}

Now we are going to use our custom cell. If you look at the cellForRow method, you will find that a UItableViewCell has been created and re used. Now all we have to do is to replace this cell with our new cell. Change the code inside this method to look like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”Cell”;

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[CustomCellallocinitWithFrame:CGRectZeroreuseIdentifier:CellIdentifier] autorelease];

}

// Set up the cell…

switch (indexPath.row) {

case 0:

cell.primaryLabel.text = @"Meeting on iPhone Development";

cell.secondaryLabel.text = @"Sat 10:30";

cell.myImageView.image = [UIImage imageNamed:@"meeting_color.png"];

break;

case 1:

cell.primaryLabel.text = @"Call With Client";

cell.secondaryLabel.text = @"Planned";

cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];

break;

case 2:

cell.primaryLabel.text = @"Appointment with Joey";

cell.secondaryLabel.text = @"2 Hours";

cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];

break;

case 3:

cell.primaryLabel.text = @"Call With Client";

cell.secondaryLabel.text = @"Planned";

cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];

break;

case 4:

cell.primaryLabel.text = @"Appointment with Joey";

cell.secondaryLabel.text = @"2 Hours";

cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];

break;

default:

break;

}

return cell;

}


I have added some dummy data. You can use your data source to provide data for primaryLabel and secondaryLabel. Please note that I have used three images here. You can use any image of your choice. All you need to to do is copy the images and paste it into your project root folder (which in this case is CustomCellTestProject folder). After pasting the files, in xcode right click on Resources (or any group), select Add >> ExistingFiles and then select all the images you want to add in you project. Once added you can simply use them by their names.
Thats it, go ahead and run the project. You will find something wrong when you compare you simulator screen with mine. If you noticed in the CustomCell.m, I have given some frames to the UI elements. You need to make sure that the height of your cell large enough to accomodate all the elements. So add this following code and you fixed the issue:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return50;

}


this is not my post the original post is here


http://blog.webscale.co.in/?p=284

Posted by 오늘마감

댓글을 달아 주세요

아이폰어플개발정보2010. 9. 11. 01:32
아이폰 어플 개발 Table View 의 기초

Table View Basics
iPhone tables(UITableView) 의 행(Row)수는 제한이 없음.(iPhone OS Memory)
UITableView - 테이블의 Data를 표시하는 뷰 오브젝트.
UITableViewCell - 테이블의 단일 행을 표현.

UITableViewCell 에 복수의 데이터를 추가하기 위해서
1.UITableViewCell 에 subview 를 추가 하거나
2.UITableViewCell 을 상속하는 서브클래스 작성.

Table > Section > Rows...
그룹 테이블(Figure 8-3)에서 각 Section 은 한 그룹을 나타냄.
인덱스 테이블(Figure 8-3)에서 각 Section 은 한 인덱스의 집합.

Implementing a Simple Table
Writing the Controller

  1. // delegate, datasource protocol 선언.
  2. // datasource 는 테이블 각 행에 대한 데이터 핸들.
  3. // delegate 는 데이터의 설정(font, user interation...) 핸들.
  4. @interface SomeUITableView  {  
  5.     // Table Data.
  6.     NSArray *tableData;  
  7.     ...  
  8. }  
  9. #pragma mark -
  10. #pragma mark Table View Data Source Methods
  11. // 해당 Section 에 행 수.
  12. - (NSInteger)tableView:(UITableView *)tableView  
  13.  numberOfRowsInSection:(NSInteger)section {  
  14.     return [self.tableData count];  
  15. }  
  16. // 행이 그려질 때 호출되어 현재 표시될 행의 UITableViewCell 를 리턴.
  17. // NSIndexPath 를 이용해서 해당 Section[indexPath section]과
  18. // 해당 행[indexPath row]을 알 수 있음.
  19. // UITableViewCell 는 캐쉬화 되고 시스템 메모리가 부족할 때 자동 메모리 해제됨.
  20. - (UITableViewCell *)tableView:(UITableView *)tableView  
  21.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  22.     // cache 할 때 구분자.
  23.     static NSString *SomeID = @"SomeID";  
  24.     // cache 에 저장된 UITableViewCell 취득.
  25.     UITableViewCell *cell = [tableView   
  26.                              dequeueReusableCellWithIdentifier:SomeID];  
  27.     if (cell == nil) {  
  28.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero  
  29.                  reuseIdentifier:SomeID] autorelease];  
  30.     }  
  31.     ...  
  32. }  


Adding an Image
UITableViewCell 의 기본 옵션으로 각 행의 왼쪽에 이미지를 붙일 수 있음.
  1. // tableview:cellForRowAtIndexPath:
  2. ...  
  3. UIImage *image = [UIImage imageNamed:@"someImage.png"];  
  4. cell.image = image;  
  5. ...  


Addtional Configurations
Setting the Indent Level
  1. #pragma mark -
  2. #pragma mark Table View Delegate Methods
  3. // 각 행의 indent level(NSInteger) 설정.
  4. - (NSInteger)tableView:(UITableView *)tableView  
  5. indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {  
  6.     ...  
  7.     return indentLevel;  
  8. }  


Handling Row Selection
USER 가 선택한 행의 이벤트 전후로 핸들링이 가능.
  1. - (NSIndexPath *)tableView:(UITableView *)tableView  
  2.   willSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
  3.     // USER 선택에 대한 이벤트 전처리.
  4.     ....  
  5. }  
  6. - (NSIndexPath *)tableView:(UITableView *)tableView  
  7.    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
  8.     // USER 선택에 대한 이벤트 후처리.
  9.     ....  
  10. }  


Changing Font Size and Row Height
  1. // tableview:cellForRowAtIndexPath:
  2. ...  
  3. cell.font = [UIFont boldSystemFontOfSize:80];  
  4. ...  
  5. // 열의 높이 설정.
  6. - (CGFloat)tableView:(UITableView *)tableView  
  7. heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
  8.     ....  
  9.     return height;  
  10. }  



출처 : http://blog.naver.com/PostList.nhn?blogId=hana_815¤tPage=91
Posted by 오늘마감

댓글을 달아 주세요