2013年2月8日金曜日

AlertViewを自動で閉じる

AlertViewにボタンを表示せずに、メッセージを表示して一定時間後に自動的に閉じる。


-(void)okOrder:(UIButton*)btn
{
    UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"" message:@"注文を受け付けました" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    //Timer 設定
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(performDismiss:) userInfo:alt repeats:NO];

    [alt show];
}

//Timer終了でアラートを閉じる
-(void)performDismiss:(NSTimer*)timer
{
    UIAlertView *alt = [timer userInfo];
    [alt dismissWithClickedButtonIndex:0 animated:NO];
}

2013年2月7日木曜日

Table にメニューを追加


#pragma UIMenuControllor

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{

    if (action == @selector(delete:)) return YES;
    if (action == @selector(change:)) return YES;
    
    return NO; //標準のメニュー項目は無効にする
}
//For menuContorollor
-(BOOL)canBecomeFirstResponder{
    return YES;
}
//Table Selected
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //選択されたセルの下にメニューを表示
   //セルの高さは100、幅は240
    int point = (indexPath.row+1)*100-20;
    [self.tableView becomeFirstResponder];
    UIMenuController *menuCont = [UIMenuController sharedMenuController];
    [menuCont setTargetRect:CGRectMake(120, point, 0, 0) inView:self.tableView];
    menuCont.arrowDirection = UIMenuControllerArrowUp;
    
    NSMutableArray *menuItem = [NSMutableArray array];
    [menuItem addObject:[[UIMenuItem alloc]initWithTitle:@"削除"
                                                  action:@selector(delete:)]];
    [menuItem addObject:[[UIMenuItem alloc]initWithTitle:@"変更"
                                                  action:@selector(change:)]];
    menuCont.menuItems = menuItem;
    [menuCont setMenuVisible:YES animated:YES];
}
//削除
-(void)delete:(id)sender
{
    NSLog(@"menu:%@", sender);
    
}
//変更
- (void)change:(id)sender
{
    
}

2013年2月6日水曜日

Custom Table Cellの作り方

New File/User Interface/Emptyを選び、CustomCell.xib
を作る。

CustomCell.xibをクリックしてStoryBoardで画面作成
   TableViewCellをドラッグ
   サイズをTableに合わせる
   その上にパーツ(ラベル、テキスト、イメージ等)を載せる

NewFileで、CustomCell.h,CustomCell.m作成

//  CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell: UITableViewCell{
}
@end

CustomCell.xibをクリック、StoryBoard
   Custom ClassをCustomCellに設定
   IdentifierにCell名設定
   Delegate, Datasourceリンク

 .hにIBOUTlet設定


//  CustomCell.h
#import <UIKit/UIKit.h>

@interface CustomCell: UITableViewCell{
    
}
@property (nonatomic,retain)IBOutlet UILabel *nameLabel;
@property (nonatomic,retain)IBOutlet UILabel *priceLabel;
@property (nonatomic,retain)IBOutlet UILabel *priceGLabel;
@property (nonatomic,retain)IBOutlet UILabel *countLabel;
@property (nonatomic,retain)IBOutlet UIImageView *image;
@property (nonatomic,retain)IBOutlet UIButton *countBtn;

@end

//  CustomCell.m
#import <UIKit/UIKit.h>
#include "CustomCell.h"
@implementation CustomCell
@synthesize nameLabel;
@synthesize priceLabel;
@synthesize priceGLabel;
@synthesize countLabel;
@synthesize image;
@synthesize countBtn;
@end

メインの.mファイルでCustom Cell設定

#import "CustomCell.h"
NSMutableArray *_objects;

#define CUSTOM_CELL_NIB @"CustomCell"
float cellheight_;

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Table delegate
    self.tableView.delegate = self;
    //Table data
    _objects = [[NSMutableArray alloc] initWithObjects:
                @"tabel1",@"table2", nil];
    
    //Table Cell Height
    UINib* nib = [UINib nibWithNibName:CUSTOM_CELL_NIB bundle:nil];
    NSArray* array = [nib instantiateWithOwner:nil options:nil];
    CustomCell* cell = [array objectAtIndex:0];
    cellheight_ =cell.frame.size.height;

}

#pragma mark - Table View

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellheight_;
}

//Table section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//Table row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return _objects.count;
}

//Init Table Data
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CUSTOM_CELL_NIB];
    
    if (cell ==nil){
        UINib* nib = [UINib nibWithNibName:CUSTOM_CELL_NIB bundle:nil];
        NSArray* array = [nib instantiateWithOwner:nil options:nil];
        cell = [array objectAtIndex:0];
    }
    
    NSDate *object = _objects[indexPath.row];
    //cell.textLabel.text = [object description];
    cell.nameLabel.text = [object description];
    
    return cell;
}

//ボタンがタップされたらテーブルに追加する
- (void)insert:(UIButton*)btn
{
    [_objects insertObject:@"hogehoge" atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}


2013年2月5日火曜日

TableView Basic

TableView ラベルのみ

Storyboard
    TableView Style = basic ラベルのみの場合
    Delegate, DataSouce 設定
    Cell Identifier = Cell 入力


NSMutableArray *_objects;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Table delegate
    self.tableView.delegate = self;
    //Table data
    _objects = [[NSMutableArray alloc] initWithObjects:
                @"tabel1",@"table2", nil];

}

#pragma mark - Table View

//Table section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//Table row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return _objects.count;
}

//Init Table Data
//セルにデータを設定する処理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    
    return cell;
}

リストがタップされたときの処理は以下の中に書く

//リストがタップされた
//iPadのとき
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

 }
}
//iPhoneのとき
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

}


//ボタンがタップされたらテーブルに追加する
//1行追加され、cellForRowAtIndexPathが呼ばれる
- (void)insert:(UIButton*)btn
{
    [_objects insertObject:@"hogehoge" atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

//1行削除
//selectedRow行が削除され

    //Update Table
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];


//データ変更
//reloadDataでcellForRowAtIndexPathが呼ばれる

    //Update Table
    [self.tableView reloadData];