squeek school #5
DESCRIPTION
TRANSCRIPT
Разработка приложений для iOSЗанятие #5
squeek school
понедельник, 16 мая 2011 г.
Табличное представление + скроллинг
• UITableView
• UIScrollView
• делегаты
понедельник, 16 мая 2011 г.
TableViewTable View Styles
UITableViewStylePlain UITableViewStyleGrouped
32Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
анатомия TableView
Table Cell
Table Footer
Table HeaderSection Header
Section Footer
Section
Table View AnatomyPlain Style
34Saturday, January 30, 2010понедельник, 16 мая 2011 г.
Table Cell
Table Footer
Table Header
Section Header
Section Footer
Section
Table View AnatomyGrouped Style
35Saturday, January 30, 2010
анатомия TableView
понедельник, 16 мая 2011 г.
использование TableView
• использование делегатов, предоставляющих данные
• просит то, что сейчас нужно
понедельник, 16 мая 2011 г.
UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)table;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
понедельник, 16 мая 2011 г.
Datasource Message Flow
Datasource
How many sections?
numberOfSectionsInTableView:
41Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
Datasource Message Flow
Datasource
tableView:numberOfRowsInSection:
How many rows in section 0?
42Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
Datasource Message Flow
Datasource
tableView:cellForRowAtIndexPath:
What to display at section 0, row 0?
43Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
NSIndexPath• массив int
• секция + строка
• [NSIndexPath indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = ...; cell.textLabel.text = [myStrings objectAtIndex:indexPath.row] return [cell autorelease];
}
понедельник, 16 мая 2011 г.
reuse - чтобы быстрее- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”];if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:@“MyIdenifier”] autorelease]; }
cell.text = [myStrings objectAtIndex:indexPath.row] return cell;
}
identifier - любой
понедельник, 16 мая 2011 г.
initWithStyle - UITableViewCellStyleCell Styles
UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
62Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
Basic properties• UITableViewCell has an image view and one or two text labels
cell.imageView.image = [UIImage imageNamed:@“vitolidol.png”];cell.textLabel.text = @“Vitol Idol”;cell.detailTextLabel.text = @“Billy Idol”;
63Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
Accessory Types
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
// Only for the blue disclosure buttonNSUInteger row = indexPath.row;...
}
// UITableView delegate method- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark
64Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
перезагрузить
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableView reloadData];}
понедельник, 16 мая 2011 г.
добавление по ходу
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
понедельник, 16 мая 2011 г.
UITableViewDelegate
• поведение + внешний вид
• логика отдельно от данных!
понедельник, 16 мая 2011 г.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
выбор строки
понедельник, 16 мая 2011 г.
обычно...
- (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];
}
понедельник, 16 мая 2011 г.
UITableViewController= UITableView + делегаты
понедельник, 16 мая 2011 г.
UIScrollViewScrolling Examples
8Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
контентContent Size
contentSize.height
contentSize.width
9Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
Content Inset
contentInset.bottom
contentInset.top
13Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
Content Inset
contentSize.height
contentSize.width
contentInset.top
contentInset.bottom
10Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
начало скролла
scrollIndicatorInsets.top
Scroll Indicator Insets
14Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
ContentOffset
15Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
contentOffset.y
contentOffset.x
17Saturday, January 30, 2010
понедельник, 16 мая 2011 г.
contentSize.height
contentSize.width
contentInset.top
contentInset.bottom
contentInset.left contentInset.right
16Saturday, January 30, 2010понедельник, 16 мая 2011 г.
как использовать?
CGRect frame = CGRectMake(0, 0, 200, 200); UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame]; frame = CGRectMake(0, 0, 500, 500); UIImage *image = [UIImage imageNamed:@"tree.jpg"]; UIImageView *myImageView = [[UIImageView alloc] initWithImage:image]; myImageView.frame = frame; [scrollView addSubview:myImageView];
scrollView.contentSize = CGSizeMake(500, 500); [self.view addSubview:scrollView];
понедельник, 16 мая 2011 г.
Делегат UIScrollViewDelegate
@protocol UIScrollViewDelegate<NSObject>@optional
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;...
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; @end
понедельник, 16 мая 2011 г.
Масштабирование
scrollView.maximumZoomScale = 2.0; scrollView.minimumZoomScale = scrollView.frame.size.width /myImage.frame.size.width;
и реализовать метод делегата:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)view {return someViewThatWillBeScaled;
}
понедельник, 16 мая 2011 г.
- (void)zoomToRect:(CGRect)rect animated:(BOOL);
Zoom to Rect
27Saturday, January 30, 2010
- (void)setZoomScale:(float)scale animated:(BOOL);
Set Zoom Scale
26Saturday, January 30, 2010
понедельник, 16 мая 2011 г.