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];


0 件のコメント:

コメントを投稿