iOS教程:如何仿一款“账本”上架app(一)

来源:互联网 发布:人工智能的产品有哪些 编辑:程序博客网 时间:2024/05/19 03:27

iOS教程:如何仿一款“账本”上架app(一)

1.  打开xcode,新建项目:new->project->single view application

2.  创建一个xib:file->new->cocoa class 勾选create xib选项。(注:在我的项目中,名字命名没太仔细,我创建的是一个继承uiview的类,名字被我命名成了viewwindowcontroller,希望注意下)

3.  在xib中,拖入控件,如图所示


这是一个主界面,点击账号按钮时,会像qq的左侧测滑栏一样的效果。

4.  自定义表视图中的cell,同样使用xib实现。


5.  下面,我们使用代码,完成表视图的操作。

其中包括textview.delegate以及textview.datasource;

(注:textview中显示的是最近的账单款项)

一开始测试时,没有接入数据库,我的数据存在plist中,它的结构是这样的


注:    self.tableview.dataSource=self;

self.tableview.delegate=self;

这两句代码一定要加进viewdidload,否则下面实现的方法全部没有用

只存了一个金钱。后面接入了数据库,没有进行优化,直接创建了一个结构相同的dictionary,里面存放了账单的具体花钱项目。一一对应,然后读取。下面直接上代码。

//获取plist文件

    /*

    NSBundle *bundle=[NSBundle mainBundle];

    NSString *plistPath=[bundle pathForResource:@"Data" ofType:@"plist"];

    self.listDict=[[NSDictionary alloc]initWithContentsOfFile:plistPath];

    */

    /*添加数组的代码

    NSArray *xjc=[NSArray arrayWithObjects:@"label",@"label2",nil ];

    NSMutableDictionary *xujunchao=[[NSMutableDictionary alloc]init];

    [xujunchao  setObject:xjc forKey:@"hai"];

    NSLog(@"%@",xujunchao);

  

     */

    //NSString *nime=_listDict[@"hello"][@"label"];

    /*self.title1=[[[NSDictionary alloc]initWithContentsOfFile:plistPath] allKeys];

   

   

     NSLog(@"%@",self.title1);

  */

 

具体需要实现的方法有如下方法:

//delegate设置行宽

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

//下面实现tableview的数据源协议

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

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

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

下面重点说说我遇到了问题:我们要实现一个功能:我们点击表视图中的其中一行,然后显示我们所点的那行的具体信息。因为我们实现的账本带有分节,所以一开始不知道从何下手。

查阅文档:

Parameters

 

tableView

A table-view object informing the delegate about the new row selection.

indexPath

An index path locating the new selected row in tableView.

 

但是我还是无从下手,因为我只知道使用row的,但是这里有section,查阅NSIndexPath的结构之后,

Parameters

row

An index number identifying a row in a UITableView object in a section identified bysection.

section

An index number identifying a section in a UITableView object.

你就可以发现,它也有section的属性,所以获取当前触摸的section,即是我们所需要的section。

那么又有一个问题了。Row是从0开始,到多少呢?是一个section从新计数,还是不单独计数呢?

通过调试,然后nslog()输出当前的row,发现是每个section都重新从0开始计算的——row。

所以代码就简单了。

通过获得的section和row,在自己创建的nsdictionary中,一个代表了key,一个代表了相应的key对应的value(一个数组)中的数组下标。

既然获取的数据了,因为这里直接是这种情况:A跳转B,所以直接属性传值。下面上代码。

注:需要跳转的界面我是直接在storyboard中创建的,所以需要跳转到storyboard中的一个界面。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    UIStoryboard *storyboard=self.storyboard;//获取当前storyboard

    NSInteger row= [indexPath row];    //获取触摸的行数

    NSIntegersection=[indexPath section];//获取出没的节

    DetailViewController*detail1;      //目标视图实例化

    detail1=[storyboard instantiateViewControllerWithIdentifier:@"detail"];//将目标视图与我们需要的视图绑定,使用这行代码,其中@“detail”identifier,在storyboard中设置

    NSString*time1=self.title1[section];//下面是获取传值数据

    NSString*monye=[[self.listDict1objectForKey:[self.title1objectAtIndex:section]]objectAtIndex:row];

    //NSLog(@"%@",self.listDict[self.title1[section]][row]);

    [self presentViewController:detail1animated:YEScompletion:Nil];//视图跳转

    detail1.label.text=self.listDict[self.title1[section]][row];//视图中相关的值的赋值

    detail1.time.text=time1;

    detail1.money.text=monye;

}

 

注:最后三行代码一定要在presentViewController:之后,不然会出现赋值失败的情况

 

此外,因为我们的这个页面是在xib中,所以需要加载到storyboard中的view中,代码如下。

    NSArray *views = [[NSBundlemainBundle]loadNibNamed:@"mainwindow"owner:selfoptions:nil];

    _mainview = views[0];

     [self.viewaddSubview:_mainview];


demo下载地址:http://www.fishxu.com/wp-content/uploads/2016/05/%E8%AE%B0%E5%B8%90%E6%9C%AC.zip


0 0