2011-03-24 61 views
1

我遇到以下問題:我想使用UITableView的單個單元作爲另一個UITableView的標題視圖。第二個表視圖又是一個從不同的UITableView類繼承的對象,因爲它完全共享它的功能。事實上唯一的區別是第二個UITableView有一個標題視圖(應該是單細胞UITableView)。UITableView作爲tableHeaderView的另一個UITableView

與頭部的UITableView的接口看起來像:

#import <UIKit/UIKit.h> 
#import "ScheduleDetailsController.h" 

@interface TodayDetailsViewController : ScheduleDetailsController <UITableViewDelegate, UITableViewDataSource> { 
    UITableView *headerView; 
} 

@property (nonatomic, retain) UITableView *headerView; 

@end 

而像執行:

#import "TodayDetailsViewController.h" 

@implementation TodayDetailsViewController 

@synthesize headerView; 

- (void)loadView { 
    [super loadView]; 

    self.headerView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    self.headerView.backgroundColor = [UIColor clearColor]; 
    self.headerView.opaque = NO; 
    self.headerView.delegate = self; 
    self.headerView.dataSource = self; 

    // This is a UITableView defined as a property in the parent class 
    self.scheduleDetailsTable.tableHeaderView = self.headerView; 
} 

... 

#pragma mark - 
#pragma mark Table View Data Source Methods 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView == self.scheduleDetailsTable.tableHeaderView) 
     return 1; 
    else 
     return [self.scheduleDetailsTable numberOfSections]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == self.scheduleDetailsTable.tableHeaderView) 
     return 1; 
    else 
     return [self.scheduleDetailsTable numberOfRowsInSection:section]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == self.scheduleDetailsTable.tableHeaderView) { 

    ... 

     return cell; 
    } else 
     return [self.scheduleDetailsTable cellForRowAtIndexPath:indexPath]; 
} 

@end 

我沒有得到任何錯誤,當我編譯和運行這個,後來我也不要什麼都看不到,即視圖保持空白。我也嘗試用UIView替換標題視圖,然後一切按預期工作(即父類中的表視圖正確實現)。我可能只是錯過了一些顯而易見的東西,但我現在無法弄清楚。如果有人能夠發現錯誤或者可以提供一些建議,我會非常感謝。

回答

0

不是沒有,但你真的需要創建一個整體 UITableViewController和 UITableView只是使用UITableViewCell作爲頭?畢竟,UITableViewCell是UIView的一個子類。

這個作品,或多或少,(要做出的標籤不不透明自己;我猜的UITableView通常處理的是):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 

    UITableViewController *testTableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain]; 

    UITableViewCell *tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NULL]; 

    tableViewCell.textLabel.text = @"Test"; 
    tableViewCell.textLabel.opaque = NO; 
    tableViewCell.detailTextLabel.text = @"123"; 
    tableViewCell.detailTextLabel.opaque = NO; 

    testTableViewController.tableView.tableHeaderView = tableViewCell; 

    [tableViewCell release]; 

    // self.window is initilized and hooked up in MainMenu.xib 
    self.window.rootViewController = testTableViewController; 
    [self.window makeKeyAndVisible]; 

    [testTableViewController release]; 

    return YES; 
} 

對於這個問題,如果上面不會滿足您的需求,爲什麼不只是一個部分磕碰一切記在你的表視圖,並使用最上面一節的「頭」:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return ([super numberOfSectionsInTableView:tableView] + 1); 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == 0) { 
     return 1; 
    } else { 
     return [super tableView:tableView numberOfRowsInSection:(section + 1)]; 
    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) { 
     // ***** Build and return your header cell ***** 
    } else { 
     NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section + 1)]; 
     return [super tableView:tableView cellForRowAtIndexPath:newIndexPath]; 
    } 
} 

必要時重複所有其他數據源的代表。

只是一個想法...

+0

感謝您的回覆。實際上,你給出的單個UITableViewCell選項確實是最好的選擇。但是,唯一的問題是,當這個單元格被選中(或者可能存在?)時,似乎不可能觸發某個操作(即推送新視圖)。另外,您給出的第二個選項對於我的情況並不適合,因爲頂部單元格需要是普通單元格,而底部表格視圖樣式需要分組。 – MJD 2011-03-25 07:59:25

+0

只是想到了自己......將UIButton作爲子視圖添加到單個UITableViewCell中以便能夠觸發某些操作是否是個好主意? – MJD 2011-03-25 08:06:37

+0

@MJD很高興你解決了你的問題。 FWIW,在整個表格視圖單元格中放置一個按鈕並跟蹤點擊可能是可以的。如果您在選擇單元格以匹配普通表格視圖時想要使用hilight,則必須自己調用[tableViewCell setSelected:YES animated:NO]以匹配預期的行爲。 (由於某些原因,當你在表格視圖中選擇一個單元格時,操作系統不會使用內置的分析...) 但是,它確實開始有點黑客 - 你可能會更好如果你需要兩種不同的風格,請按照你的方式做... – peterjb 2011-03-26 02:43:35

0

嗯,我解決了這個問題由

return [self.scheduleDetailsTable cellForRowAtIndexPath:indexPath]; 

替換我原來張貼的委託方法的返回語句

return [super tableView:tableView cellForRowAtIndexPath:indexPath]; 

而且對於其他代表方法也是如此。現在一切都按預期工作。感謝peterjb在回答我的原始問題時使用了這種語法。

+0

我想做一些非常類似於EKEventViewController的東西。但它不會編譯,因爲EKEventViewController沒有在其接口中定義的委託方法。有任何想法嗎? – roocell 2012-09-21 17:59:53

相關問題