본문 바로가기

PDA&Mobile

아이폰 앱 개발 팁(7) : Stanford Lecture #8

* 본 포스트는 Blog.MissFlash.com에서 작성한 것으로, 원문 저작자의 동의없이 마음대로 퍼가실 수 없습니다. 포스트의 내용이 마음에 드시면 링크를 이용해주시면 감사하겠습니다.

> Stanford Lecture #8

* Using a Scroll View
// Create with the desired frame
CGRect frame = CGRectMake(0, 0, 200, 200);

// Add subviews
frame = CGRectMake(0, 0, 500, 500);
myImageView = [[UIImageView alloc] initWithFrame:frame];
[scrollView addSubview:myImageView];

// Set the content size
scrollView.contentSize = CGSizeMake(500, 500);

* UIScrollView delegate
@protocol UIScrollViewDelegate<NSObject>
@optional
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
...
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
@end

* Zooming with a Scroll View
scrollView.maximumZoomScale = 2.0;
scrollView.minimumZoomScale = scrollView.size.width / myImage.size.width;
- (UIView *)viewForZoomingInScrollView:(UIView *)view{
return someViewThatWillBeScaled;
}

* Provide number of sections and rows
- (NSInteger)numberOfSectionsInTableView:(UITableView *)table;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

* Provide cells for table view as needed
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

* Category on NSIndexPath with helper methods
+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;
@property(nonatomic,readonly) NSUInteger section;
@property(nonatomic,readonly) NSUInteger row;
@end

* Cell reuse
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

* Triggering updates
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableView reloadData];
}

* Customize appearance of table view cell
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

* Validate and respond to selection changes
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

* Responding to selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = indexPath.row;
id objectToDisplay = [myObjects objectAtIndex:row];

MyViewController *myViewController = ...;
myViewController.object = objectToDisplay;

[self.navigationController pushViewController:myViewController animated:YES];
}

* Altering or disabling selection
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == ...){
return nil;
}else{
return indexPath;
}
}

* Accessory types
- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = indexPath.row;
...
}

* Add additional views to the content view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = ...;
CGRect frame = cell.contentView.bounds;

UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
myLabel.text = ...;
[cell.contentView addSubview:myLabel];

[myLabel release];
}

* Custom row heights
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text = ...;
UIFont *font = [UIFont systemFontOfSize:...];
CGSize withinSize = CGSizeMake(tableView.width, 1000];

CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + somePadding;
}