2012-03-25 14 views
0

私はUITableViewを持っています。大量のデータが多数の行で表示され、セクションを作成したい(デフォルトの連絡先アプリケーションとそのセクションのように)。だから私のコード(listViewController.mファイル)があります:SQLiteデータベースを含むテーブルビューでセクションを実装するには?

#import "FailedBanksListViewController.h" 
#import "FailedBankDatabase.h" 
#import "FailedBankInfo.h" 
#import "FailedBanksDetailViewController.h" 
#import "BIDAppDelegate.h" 

@implementation FailedBanksListViewController 
@synthesize failedBankInfos = _failedBankInfos; 
@synthesize details = _details; 


- (void)viewDidLoad { 

    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"3.png"]];; 
    [super viewDidLoad]; 
    self.failedBankInfos = [FailedBankDatabase database].failedBankInfos; 
    self.title = @"Продукты"; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    self.failedBankInfos = nil; 
    self.details = nil; 

} 
- (void) viewWillAppear:(BOOL)animated 
{ 


} 

#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return [_failedBankInfos count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [_failedBankInfos count]; 
} 


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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 


    // Set up the cell... 
    FailedBankInfo *info = [_failedBankInfos objectAtIndex:indexPath.row]; 
    cell.textLabel.text = info.name; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", info.city, info.state]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (self.details == nil) { 
     self.details = [[FailedBanksDetailViewController alloc] initWithNibName:@"FailedBanksDetailViewController" bundle:nil];   
    } 
    FailedBankInfo *info = [_failedBankInfos objectAtIndex:indexPath.row]; 
    _details.uniqueId = info.uniqueId; 
    [self.navigationController pushViewController:_details animated:YES]; 
} 

- (void)dealloc { 
    self.failedBankInfos = nil; 
} 


@end 
+0

これはあなたのコードです。あなたの問題は何ですか? – Matthias

+0

私はTableViewでセクションを作りたいと思っていますが、SQliteで作業していない例はすべて、単純なNSarraysです。最初の文字だけでなく、「食べ物」、「肉」、「魚」などのセクションが必要です。 – Necrosoft

+0

何を試しましたか?どこに問題があったのですか?シンプルな "あなたのコードを教えてください"と言っているのはうまくいきません。 – Matthias

答えて

0

あなたのコードを使用すると、複数のセクション(他のものより正確に等しい各1)を持っている必要があります。 複数セクションのテーブルビューのアイデアは、(通常は)2次元配列(あなたの場合のように1次元ではありません)です。次に、各行は、テーブルビューのセクションを表します。あなたはこのように構造化配列を持っている場合

例えば、(と私はあなたがそれをこのように初期化することはできません知っている):

arr = { 
    {'apple','orange','banana'}, 
    {'CD-Rom', 'DVD', 'BR-Disk'}, 
    {'AK-47', 'Rocket launcher', 'Water gun'} 
} 

セクション方法のあなたの数が[arr count]と行数のためを返すことがあります。セクションsは[[arr objectAtIndex:s] count]を返します。また、テーブルビューのデータソースメソッドtableView:titleForHeaderInSection:を使用して、各セクションのタイトルを設定できることに注意してください。

SQLite DBから情報をロードする場合は、何も変更されません。それはまったく同じですが、データを取得する方法を維持する必要があります。

あなたがこのすべてのことを理解したら、Core Data frameworkをチェックアウトしてください。

+0

ありがとうRicard – Necrosoft

関連する問題