2012-03-25 27 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 2012-03-25 18:52:23

+0

我想在TableView中創建各個部分,我所看到的所有示例都不適用於SQlite,只是簡單的NSarrays。我想要部分內容 - 「食物」,「肉」,「魚」等,不只是第一個字母。 – Necrosoft 2012-03-25 18:55:09

+0

你試過了什麼?問題在哪裏?簡單的說「給我你的代碼」在SO上不起作用。 – Matthias 2012-03-25 18:58:34

回答

0

與您的代碼,你應該有多個部分(每一個都比其他人完全相同)。 對於多節表格視圖的想法是(通常)有一個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數據庫加載信息,什麼都不會改變。它完全一樣,但你將不得不保持獲取你的數據的方式。

當你理解了所有這些東西,然後結賬Core Data framework

+0

謝謝Ricard – Necrosoft 2012-03-25 19:39:22

相關問題