iOS 5 Storyboard 學習之 Tabbar Controller,Navigation Controller (2) 代碼部分


繼續《iOS 5 Storyboard 學習之 Tabbar Controller,Navigation Controller (1)》的內容,如果想從頭學習Storyboard,請參考iOS 5 Storyboard 學習之 UITableViews

1 建立文件“ PlayersViewController”記得選擇”UITableViewController

屏幕快照 2012-03-09 上午11.12.19.png

2 選擇“Table View Controller”設置“Identity Inspector”的Class為“PlayersViewController

屏幕快照 2012-03-07 下午2.33.20.png

編輯“ PlayersViewController.h”,建立一個可修改的數組,這個將是我們今后存儲Player數據的地方

#import <UIKit/UIKit.h>

 

@interface PlayersViewController : UITableViewController

 

@property (nonatomic,strong) NSMutableArray *players;

 

@end

3 先放一放,建立一個基於“Objective-C class template”的Player文件,他的Subclass是“NSObject”

屏幕快照 2012-03-07 下午2.34.40.png

然后編輯“Player.h”和“Player.m

Player.h

#import <Foundation/Foundation.h>

 

@interface Player : NSObject

 

@property (nonatomic,copy)NSString *name, *game;

@property (nonatomic,assign) int rating;

 

@end

 

Player.m

#import "Player.h"

 

@implementation Player

@synthesize name,game,rating;

 

@end

4 接下來在App Delegate中作些動作,首先在AppDelegate.m中import“ Player.h”和“ PlayersViewController.h

編輯“AppDelegate.m

#import "AppDelegate.h"

#import "Player.h"

#import "PlayersViewController.h"

 

@implementation AppDelegate {

NSMutableArray *players;

}

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

players = [NSMutableArray arrayWithCapacity:20];

  

Player *player = [[Player alloc] init];

player.name = @"xxd";

player.game = @"游泳";

player.rating = 4;

[players addObject:player];

  

player = [[Player alloc] init];

player.name = @"張三";

player.game = @"足球";

player.rating = 5;

[players addObject:player];

  

player = [[Player alloc] init];

player.name = @"李四";

player.game = @"籃球";

player.rating = 2;

[players addObject:player];

  

//此為storyboard的局限,在Interface Builder時代,在MainWindow.xib中可以鏈接view controllersApp Delegateoutlets,但是現在不可以,只能寫代碼設置第一個controller是什么,或者誰知道有什么辦法請告知,謝謝。

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

  

//同樣UINavigationController也只能這樣設置,指定UINavigationControllertabBarController的第一個顯示

UINavigationController *navigationController = [[tabBarController viewControllers] objectAtIndex:0];

  

//指定PlayersViewControllernavigationController的第一個顯示

PlayersViewController *playersViewController = [[navigationController viewControllers] objectAtIndex:0];

 

 

//上邊的三句都是為了設定這一句而寫,因為不寫默認就是上邊的順序,但是不寫就沒有這個和players鏈接的接口了

playersViewController.players = players;

  

return YES;

}

5 修改item1下的數據顯示,修改“PlayersViewController.m

#import "PlayersViewController.h"

#import "Player.h"

 

@implementation PlayersViewController

@synthesize players;

 

 

#pragma mark - Table view data source

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

 

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

{

return [self.players count];

}

 

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

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];

Player *player = [self.players objectAtIndex:indexPath.row];

cell.textLabel.text = player.game;

cell.detailTextLabel.text = player.game;

return cell;

}

目前俄顯示結果應該是這樣的


屏幕快照 2012-03-09 下午1.54.15.png

6 下面來看看我們能在“Prototype Cells”中做些什么

設置Table View高為55

屏幕快照 2012-03-09 下午2.35.14.png

托一個UiImageView到Cell上,然后設置Cell行高
屏幕快照 2012-03-09 下午2.07.19.png

設置出右邊的箭頭
屏幕快照 2012-03-09 下午2.18.11.png

設置ImageView的寬度
屏幕快照 2012-03-09 下午2.13.02.png

最后測試別較好的大小是:x = 199,Y = 13, Width = 81, Height = 28

然后我們配置Cell,基於“Objective-C class template建立一個PlayerCell文件,他的Subclass是“NSObject”

PlayerCell.h

#import <Foundation/Foundation.h>


@interface PlayerCell : UITableViewCell


@property(nonatomic, strong) IBOutlet UILabel *nameLabel,*gameLabel;

@property(nonatomic, strong) IBOutlet UIImageView *ratingImageView;


@end


PlayerCell.m

 

#import "PlayerCell.h"


@implementation PlayerCell


@synthesize nameLabel,gameLabel;

@synthesize ratingImageView;


@end

點擊MainStoryboard.storyboard,點擊 prototype cell修改他的Class為“ PlayerCell


屏幕快照 2012-03-09 下午2.45.21.png

 

然后在“PlayersViewController.m”中添加

#import "PlayerCell.h"

在“PlayersViewController.m”中修改

 

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

{

PlayerCell *cell =(PlayerCell *)[tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];

Player *player =[self.players objectAtIndex:indexPath.row];

cell.nameLabel.text = player.name;

cell.gameLabel.text = player.game;

cell.ratingImageView.image =[self imageForRating:player.rating];

return cell;

}

隨后在“PlayersViewController.h”中添加

-(UIImage *)imageForRating:(int)rating;

在“PlayersViewController.h”中添加

-(UIImage *)imageForRating:(int)rating

{

switch(rating)

{

case1:return[UIImage imageNamed:@"1StarSmall.png"];

case2:return[UIImage imageNamed:@"2StarsSmall.png"];

case3:return[UIImage imageNamed:@"3StarsSmall.png"];

case4:return[UIImage imageNamed:@"4StarsSmall.png"];

case5:return[UIImage imageNamed:@"5StarsSmall.png"];

}

return nil;

}

運行看看什么模樣


屏幕快照 2012-03-09 下午3.44.12.png

7 增加刪除功能,在“ PlayersViewController.m”添加下邊代碼

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath

{

if(editingStyle == UITableViewCellEditingStyleDelete)

{

[self.players removeObjectAtIndex:indexPath.row];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

}

調整一下,讓星星能夠自動躲避
屏幕快照 2012-03-09 下午3.47.04.png

看下顯示效果


屏幕快照 2012-03-09 下午3.48.16.png

今天就到這里,明天繼續。下一篇iOS 5 Storyboard 學習之 Tabbar Controller,Navigation Controller (3) 深入Segue,Class,Protocol,Delegate的基本使用

--EOF--


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM