从零开始学习iOS开发-股票记帐本1.0(1)
发布日期:2022-02-01 13:46:24 浏览次数:37 分类:技术文章

本文共 2741 字,大约阅读时间需要 9 分钟。

自己的第一个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联络(可以参考第9章)。

1)在B.h中定义protocol

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

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

@property (weak, nonatomic)id
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;    }}

转载地址:https://blog.csdn.net/shidesheng5435/article/details/47711741 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:《Objective-C基础教程》第17章 文件加载和保存
下一篇:从零开始学习iOS开发-股票记帐本1.0(2)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月14日 20时34分07秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分表解决方案(一) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之关联查询解决方案(三) 2019-04-27
Linux文件管理参考 2019-04-27
FTP文件管理项目(本地云)项目日报(二) 2019-04-27
FTP文件管理项目(本地云)项目日报(三) 2019-04-27
FTP文件管理项目(本地云)项目日报(七) 2019-04-27
FTP文件管理项目(本地云)项目日报(九) 2019-04-27
以练代学设计模式 -- FTP文件管理项目 2019-04-27
FTP文件管理项目(本地云)项目日报(十) 2019-04-27
学以致用设计模式 之 “组合模式” 2019-04-27
我用过的设计模式(7)--享元模式 2019-04-27
MySQL数据库从入门到实战应用(学习笔记一) 2019-04-27
MySQL数据库从入门到实战应用(学习笔记二) 2019-04-27
种树:二叉树、二叉搜索树、AVL树、红黑树、哈夫曼树、B树、树与森林 2019-04-27
【C++】攻克哈希表(unordered_map) 2019-04-27
转:【答学员问】- 该如何根据岗位学习相关技能 2019-04-27
转:【答学员问】有什么经验教训,是你在面试很多次之后才知道的? 2019-04-27
消息队列:解耦、异步、削峰,现有MQ对比以及新手入门该如何选择MQ? 2019-04-27
【奇技淫巧】-- 三角形最小路径和 2019-04-27