从零开始学习iOS开发-股票记帐本1.0(1)

来源:互联网 发布:声鉴软件 编辑:程序博客网 时间:2024/05/01 14:42

自己的第一个App的1.0版本终于新鲜出炉了(虽然现在还在审核中)。
下面将开发中遇到的问题做一个小小的纪录吧。

1. Table View在ViewController中的使用

1) 在storyboard中,设置tableview的datasource和delegate
2) .h文件中,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

如果是使用默认的cell,则

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *cellIdentifier=@"cellIdentifier";    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier ];    if (cell==nil) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];    }    cell.textLabel.text=@"hello";    return cell;}

2. 导入自定义nib文件

在viewDidLoad中

    UITableView *tableView=(id)[self.view viewWithTag:1];    tableView.rowHeight=80;    UINib *nib=[UINib nibWithNibName:@"stockTradeTableViewCell" bundle:nil];    [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];

此外,还需要在

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

中定义

stockTradeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

3. 代理模式

A窗口打开了B窗口,而在程序进行中B想与A联络(可以参考《iOS Learning 2》第9章)。
1)在B.h中定义protocol

@protocol addStockViewControllerDelegate <NSObject>- (void)addStockViewControllerDidCancel:(addStockViewController *)controller;- (void)addStockViewController:(addStockViewController *)controller didFinishAddingStockData:(stockData *)stockdata;@end

2)在B.h中定义代理协议的变量属性

@property (weak, nonatomic)id<addStockViewControllerDelegate>delegate;

3)让B在合适的时候向A发送消息,即在恰当的位置使用protocol中定义的方法

[self.delegate addStockViewController:self didFinishAddingStockData:stockdata ];

4)让A遵从代理协议,在@interface后添加;并在.m文件中添加方法描述
5)通知B,现在A现在是B的代理了

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    if ([segue.identifier isEqualToString:@"addStock"]) {        UINavigationController *navigationController=segue.destinationViewController;//新的视图控制器可以在segue.destinationViewController中找到        addStockViewController *controller=(addStockViewController *)navigationController.topViewController;//为了获取addStockViewController对象,我们可以查看导航控制器的topViewController属性,该属性指向导航控制器的当前活跃界面        controller.delegate=self;//一旦我们获得了到addStockViewController对象的引用,就需要将delegate属性设置为self(这样在addStockViewController中的self.delegate才是stockTradeViewController),而self指的是stockTradeViewController    }    else if([segue.identifier isEqualToString:@"sellStock"]){        sellStockViewController *controller=segue.destinationViewController;        controller.delegate=self;        controller.stockdata=sender;    }}
0 0