iosdatasource怎么写

1.ios创建一个表格需要使用哪些delegate和datasource在MVC中,这种实现常用在设计View的时候.为了实现一个通用view的编写,降低耦合度,并按照UIKit的一贯习俗(例如UITableView),而设计为用delegate告知view的使用者某些事件的到来,用datasource获取必要的数据.看一个例子:View:MyView.h#[email protected] MyViewDelegate;@protocol MyViewDatasource;@interface MyView : [email protected] (nonatomic, assign) id myViewDelegate;@property (nonatomic, assign) id myViewDatasource;- (void)showAlert;@end/////////////////////////////////////////////@protocol [email protected] (void)alertDidPop:(UIView *)myView;@end/////////////////////////////////////////////@protocol MyViewDatasource// the default is @required- (NSString *)textOfAlert;@endMyView.m#import "MyView.h"@implementation [email protected] 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 : [email protected]#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.
这个方法设置每个每组里面有两个子项 。
【iosdatasource怎么写】- (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添加事件代理和数据源代理