2009-07-15 15 views
4

私は、ユーザが検索バーに入力した変数に基づいて、自分のサイトからデータを取得するNSURLオブジェクトを持っています。動的に変化するUITableViewのコンテンツ

このデータをNSArrayに分割します。

これを済ませたら、データをUITableViewに表示したいと思います。

私の質問はこれです。 UITableViewにデータを動的にロードすることは可能ですか?

つまり、プログラムがロードされ、UITableViewが空になるようなデータがない場合、ユーザーは1つの変数を検索します。いくつかのデータを取得し、その内容がUITableViewにロードされます。新しい変数を検索し、古いデータがUITableViewからクリアされ、新しいデータが追加されますか?

私は現在インターフェイスビルダーを使ってこれをやろうとしていますが、実際にインターフェイスを作成して、UITableViewを破棄して再作成する必要があるかもしれないと懸念しています。

ありがとうございました。

答えて

7

は確かにトリックに

+0

は私がreloadDataを使用することができるとは思わない理由として、私の最初の投稿に説明を追加するために行く省略することができます。 – JonB

+0

さらに検査し、私がしたいことに関するチュートリアルを見つけたら、reloadDataを使用しないことを正当化しようとすると非常に馬鹿に見えます。 ご迷惑をおかけして申し訳ありません。 – JonB

3

恐怖を行いますのUITableViewの方法reloadDataはない、サブクラスのUITableViewは非常に簡単です。 xCodeでは単に新しいファイルを選択し、 "Cocoa Touch Classes"、 "Objective-c class"、 "Subclass of"ドロップダウンで "UITableView"を選択します。 xCodeは、ビルドするスタブを含むUITableViewControllerサブクラスを追加します。

非常に単純な例では、配列からテーブルデータを描画し、アプリケーション代理人から表示されます。 reloadDataメッセージをUITableViewに送信することをお勧めしましたので、表示されたデータが更新されます。

おそらくあなたが知ったように、この仕事にInterfaceBuilderを使用することは、プログラム的に行うよりもはるかに困難です。

乾杯、ニールス・

// 
// MyTableViewController.m 
// TableView 
// 
// Created by Niels Castle on 7/15/09. 
// Copyright 2009 Castle Andersen ApS. All rights reserved. 
// 

#import "MyTableViewController.h" 


@implementation MyTableViewController 

// Initializer do custom initialisation here 
- (id)initWithStyle:(UITableViewStyle)style { 
    if (self = [super initWithStyle:style]) { 

     // This is the source of my data. The simplest source possible, 
     // an NSMutableArray, of name. This would be the data from your web site 
     array = [[NSMutableArray alloc] 
     initWithObjects:@"Niels", @"Camilla", @"William", nil]; 
    } 
    return self; 
} 


// How many section do we want in our table 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view 
// Simply the number of elements in our array of names 
- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section { 
    return [array count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Reuse cells 
    static NSString *id = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] 
      initWithStyle: UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Simplest possible cell - displaying a name from our name array 
    [[cell textLabel] setText: [array objectAtIndex:[indexPath row]]]; 

    return cell; 
} 

- (void)dealloc { 
    [super dealloc]; 
    [array release]; 
} 

@end 


// 
// TableViewAppDelegate.m 
// TableView 
// 
// Created by Niels Castle on 7/15/09. 
// Copyright Castle Andersen ApS 2009. All rights reserved. 
// 

#import "TableViewAppDelegate.h" 
#import "MyTableViewController.h" 

@implementation TableViewAppDelegate 

@synthesize window; 


- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    MyTableViewController *twc = [[MyTableViewController alloc] 
     initWithStyle: UITableViewStylePlain]; 
    [window addSubview: [twc view]]; 

    [window makeKeyAndVisible]; 
} 


- (void)dealloc { 
    [window release]; 
    [super dealloc]; 
} 


@end 
0

それは少し複雑だが、確実に非常に働いている私の解決策は以下の通りです: (あなたは、各セクションを表し、含まれていることを、配列の束として配列を持っていることを前提としテーブル内の実際の行である項目)。

この例では、サーバー(JSONなど)からデータを読み込む際に、結果がセクションや行の数が大きく異なる場合があります。

void関数、あなたはそれ

-(void)addToPropertiesTable { 

    //fullTableData is above mentioned two dimensional array 
    int sectionsCount = _fullTableData.count; 
    int count = 0; 
    NSMutableArray *insertIndexPaths = [NSMutableArray array]; 
    NSMutableArray *deleteIndexPaths = [NSMutableArray array]; 

    for(int j = 0; j < sectionsCount; j++) { 
     NSMutableArray *currentAdverts = [[NSMutableArray alloc] init]; 
     [currentAdverts addObjectsFromArray:[_fullTableData objectAtIndex:j]]; 
     count = [currentAdverts count]; 

     int currentRowsInSection = [self.propertiesTable numberOfRowsInSection:j]; 

     if(currentRowsInSection > 0) { 
      //if any data in current tableView, lets get rid of them first 
      for (int i = [self.propertiesTable numberOfRowsInSection:j] - 1; i >=0 ; i--) 
      { 
       [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:j]]; 
      } 
     } 
     for (NSUInteger item = 0; item < count; item++) {    
      [insertIndexPaths addObject:[NSIndexPath indexPathForRow:item inSection:j]]; 
     } 
    } 


    [self.propertiesTable beginUpdates]; 

    //we delete old rows - whether we need them or not 
    [self.propertiesTable deleteRowsAtIndexPaths:deleteIndexPaths 
           withRowAnimation:UITableViewRowAnimationFade]; 
    if([self.propertiesTable numberOfSections]) { 

     //if any sections, we remove them 
     NSIndexSet *nsIndexSetToDelete = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[self.propertiesTable numberOfSections])]; 
     [self.propertiesTable deleteSections:nsIndexSetToDelete withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 


    //here we have to set new sections, whether they have changed or not 
    NSIndexSet *nsIndexSetToInsert = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,sectionsCount)]; 
    [self.propertiesTable insertSections:nsIndexSetToInsert withRowAnimation:UITableViewRowAnimationAutomatic]; 

    //finally we insert rows 
    [self.propertiesTable insertRowsAtIndexPaths:insertIndexPaths 
           withRowAnimation:UITableViewRowAnimationFade]; 
    [self.propertiesTable endUpdates]; 

    //now we see the change in UI 
    [self.propertiesTable reloadData]; 
} 
関連する問題