2011-04-21 19 views
0

我subclassed UITableViewController並設置一個UITableView與GroupedStyle作爲它的子視圖。當我在模擬器中運行我的項目時,節標題出現兩次:在GroupedStyle和PlainStyle中。有沒有人有一個想法,爲什麼發生這種情況?我附加了我認爲可能是下面的罪魁禍首的功能。UITableViewStyleGrouped標題爲分組和平原

2 section headers in Simulator

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
[self setup]; 
[email protected]"Literature"; 
UITableView *tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; 
tableview.scrollEnabled=NO; 
tableview.delegate=self; 
tableview.dataSource=self; 
[self.view addSubview:tableview]; 
[tableview release]; 
return self; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
NSInteger inte=2; 
switch (section) { 
    case 0: 
     inte=2; 
     break; 
    case 1: 
     inte=4; 
     break; 
} 
return inte; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
return 2; 
} 
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
NSString *temp; 
switch (section) { 
    case 0: 
     [email protected]"Literature"; 
     break; 
    case 1: 
     [email protected]"Online"; 
     break; 
} 
return temp; 
} 

回答

1

的UITableViewController爲您創建一個表視圖,它設置爲一個名爲的tableView財產。它還將委託和數據源設置爲自己。所以,你必須做的所有這些代碼行都是多餘的。

問題出在創建新的表格視圖並將其添加爲子視圖。因此,您現在有2個表視圖,即您作爲子視圖添加的視圖,以及作爲tableView屬性創建的UITableViewController。

我懷疑你得到的顯示是兩個重疊的表視圖的結果。其中一個表視圖是您設置爲GroupedStyle的視圖,另一個視圖是默認的PlainStyle。

您可以參考UITableViewController class documentation

+0

你太棒了! :) 非常感謝。現在一切正常。 :) – Julian 2011-04-21 07:44:46

+1

哈哈,很高興它解決了 – 2011-04-21 07:53:28