2011-09-28 26 views
0

我已使用New File對話框中的UITableViewController選項使用預設創建了UITableView。我使用Interface Builder將樣式設置爲分組。UITableView始終處於純文本模式,但在XIB中另行定義

但是,表總是顯示在plain style

數據源由兩部分組成,每部分包含兩個項目和一個部分標題。這一切都顯示出來,但風格是錯誤的。通過NSLog我驗證樣式在運行時真的被設置爲純文本。我錯過了什麼嗎?

編輯:在這裏我的代碼。正如我所提到的NSLog調用返回的預期值。

@implementation EventTableView 

@synthesize tableView = _tableView; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[self navigationItem] setTitle:@"Events"]; 
    self.clearsSelectionOnViewWillAppear = YES; 

    NSLog(@"Table view style: %@.", (self.tableView.style == UITableViewStylePlain ? @"Plain" : @"Grouped")); 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (_appDelegate == nil) { 
     _appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate]; 
    } 
    return _appDelegate.model.eventSections.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    Section* eventSection = [_appDelegate.model.eventSections objectAtIndex:section]; 
    NSInteger result = [eventSection getCountAsInteger]; 
    if (eventSection != nil && result >= 0) { 
     NSLog(@"Got %d rows in event section %d.", result, section); 
     return result; 
    } else { 
     NSLog(@"Can't get event section row count. Defaulting to 0."); 
     return 0; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"EventCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = @"Test"; 
    return cell; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    Section* eventSection = [_appDelegate.model.eventSections objectAtIndex:section]; 
    return eventSection.name; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Pressed row..."); 
} 

@end 

編輯:爲了幫助的東西,包括我的Interface Builder(樣式設置爲group)的屏幕截圖。 Interface Builder

+0

請顯示一些代碼。你如何在你的應用程序中實例化tableview? – uvesten

+0

由於對象來自XIB,我沒有真正實例化任何東西... – spa

回答

0

這真的很瘋狂。我發現了一種工作。

我添加了一個標準UIViewController並在其上實施了協議UITableViewDelegateUITableViewDataSource

在XIB/NIB中,我刪除了標準UIView並添加了UITableView。我將視圖的插座datasourcedelegate連接到文件所有者對象,並將文件所有者的視圖插座連接到UITableView

現在我可以設置表的風格進行分組。到目前爲止,一切都很好。然而,我不明白使用UITableViewController ...

1

更新時間:

查找

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style 
在你的控制器

,設置有style = UITableViewStyleGrouped;打電話來super

+0

不,不起作用。這是一個只讀屬性。 – spa

+0

已更新,請檢查它 – Nekto

+0

還是:-)此init方法不可用。我檢查所有可用的init方法並添加了一個NSLog。沒人會被叫到。我猜是因爲該對象從NIB/XIB反序列化。 – spa

1

如果沒有地方的任何代碼調用之前- (id)initWithStyle:(UITableViewStyle)style你的觀點與錯誤的風格(這種我期望)的方法, 你可以重寫你的init方法來

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    return self; 
} 

不過,首先,你可以嘗試以確保您保存您的.xib在Xcode中,清潔和重建您的應用程序,讓你確保你的變化是什麼實際上在設備/模擬器上運行。

+0

沒有抱歉。如前所述,我檢查了所有可用的init方法並添加了NSLog。沒人會被叫到。我猜是因爲該對象從NIB/XIB反序列化。 – spa

+0

您是否嘗試更改init方法? – uvesten

+0

是的,我做了......當然它沒有效果,因爲它從來沒有被稱爲... – spa

相關問題