1.ios创建一个表格需要使用哪些delegate和datasource
在MVC中, 这种实现常用在设计View的时候.为了实现一个通用view的编写, 降低耦合度, 并按照UIKit的一贯习俗(例如UITableView), 而设计为用delegate告知view的使用者某些事件的到来, 用datasource获取必要的数据.看一个例子:View:MyView.h#import@protocol MyViewDelegate;@protocol MyViewDatasource;@interface MyView : UIView@property (nonatomic, assign) id myViewDelegate;@property (nonatomic, assign) id myViewDatasource;- (void)showAlert;@end/////////////////////////////////////////////@protocol MyViewDelegate@optional- (void)alertDidPop:(UIView *)myView;@end/////////////////////////////////////////////@protocol MyViewDatasource// the default is @required- (NSString *)textOfAlert;@endMyView.m#import "MyView.h"@implementation MyView@synthesize myViewDelegate;@synthesize myViewDatasource;- (void)showAlert{ UIAlertView *myAlertView = [UIAlertView new]; myAlertView.delegate = self; myAlertView.message = [self.myViewDatasource textOfAlert]; [myAlertView show]; [myAlertView release];}// after animation- (void)didPresentAlertView:(UIAlertView *)alertView{ [self.myViewDelegate alertDidPop:self];}@endController:ViewController.h#import#import "MyView.h"@interface ViewController : UIViewController@endViewController.m#import "ViewController.h"@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; MyView *myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)]; myView.myViewDatasource = self; myView.myViewDelegate = self; [self.view addSubview:myView]; [myView showAlert]; [myView release];}- (void)alertDidPop:(UIView *)myView{ myView.backgroundColor = [UIColor yellowColor];}- (NSString *)textOfAlert{ return @"message from controller";}@end。
2.这个界面如何用ios代码实现
最上面的导航栏是UINavigationControll
中间是用UITableView实现的。
最下面就是UILabel。
UITableView 的 dataSource 委托里有
- (NSInteger):(UITableView *)tableView; 这个方法。默认是返回1,上面那个界面有两组,是返回2.
这个方法设置每个每组里面有两个子项。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
然后用这个方法来定制每个项的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
百度一下 UITableView 能找到很多的代码,这里就不复制了,自己解决问题会记得比较牢固。
3.IOS代码是怎样和界面关联起来的
最上面的导航栏是UINavigationControll
中间是用UITableView实现的。
最下面就是UILabel。
UITableView 的 dataSource 委托里有
- (NSInteger):(UITableView *)tableView; 这个方法。默认是返回1,上面那个界面有两组,是返回2.
这个方法设置每个每组里面有两个子项。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
然后用这个方法来定制每个项的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
百度一下 UITableView 能找到很多的代码,这里就不复制了,自己解决问题会记得比较牢固。
4.ios纯代码怎么写的identifier
方法/步骤创建工程项目和视图控制器 创建工程项目UICollectionView,新建一个UIViewController。
选中工程,右键-New File…选择“Cocoa Touch Class”-Next,给个合理的名称ViewController,再Next完成。 在AppDelegate.m文件包含#import "ViewController.h"。
添加代码: *navC = [[ alloc]:[[ViewController alloc]init]]; self.window.rootViewController = navC;//将navC设置为根视图控制器。修改一下ViewController的显示样式,执行编译,run一下,效果如图。
创建自定义UICollectionViewCell 选中工程,右键-New File…选择“Cocoa Touch Class”-Next,选择继承于UICollectionViewCell类,给个合理的名称CollectionViewCell,再Next完成。 1、自定义所需要的控件,比如UIImageView:@property(nonatomic ,strong)UIImageView *imgView; 2、初始化控件,在方法- (id)initWithFrame:(CGRect)frame中实现:self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 30, 150, 140)];self.imgView.backgroundColor = [UIColor ];[self addSubview:self.imgView];实现初始化UICollectionView方法 1、在ViewController.h添加事件代理和数据源代理<,>。
2、在ViewController.m创建UICollectionView。需要使用来创建,使用方法- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:( *)layout;如果只用普通的init方法,是实现不了的。
3、设置flowLayout的属性。 4、初始化CollectionViewCell和头部。
实现UICollectionView的Delegate、DataSource方法 1、返回Items个数:collectionView: : 2、返回Sections个数:: 3、返回Cell显示内容:collectionView: : 4、返回头部尾部显示内容:collectionView: : atIndexPath: 5、选中时调用的方法:collectionView: :显示CollectionView及设置数据源 在viewDidLoad方法内:[self.view addSubview:self.collectionView];代码self.collectionView会自动调用setter、getter方法。即调用- (UICollectionView *)collectionView方法初始化并返回collectionView。
然后addSubview:到self.view上。配合广告栏和定时器,完成显示。
转载请注明出处育才学习网 » iosdatasource怎么写