开发者

UITableView as a tableHeaderView for another UITableView

I get stuck on the following: I want to use a single cell of a UITableView as a header view for another UITableView. The second table view is in turn an object that has been inherited from a different UITableView class, since it shares it's functionality completely. The only difference in fact is that the second UITableView has a header view (which should be the single celled UITableView).

The interface for the UITableView with the header looks like:

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

@in开发者_Python百科terface TodayDetailsViewController : ScheduleDetailsController <UITableViewDelegate, UITableViewDataSource> {
   UITableView *headerView;
}

@property (nonatomic, retain) UITableView *headerView;  

@end

And the implementation like:

#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

I get no errors when I compile and run this, but then I also don't see anything, i.e. the view stays empty. I have also tried it with replacing the header view by a UIView and then everything works like expected (i.e. the table view in the parent class is implemented correctly). I'm probably just missing something obvious, but I just can't figure it out at the moment. If someone could spot the error or could give some advice I would be very thankful.


Not for nothing, but do you really need to create a whole UITableViewController and UITableView just to use a UITableViewCell as a header? A UITableViewCell is a subclass of UIView, after all.

This works, more or less, (had to make the labels not opaque myself; I guess the UITableView normally handles that):

- (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;
}

For that matter, if the above wont meet your needs why not just bump everything down in your table view by one section, and use the very top section as the "header":

- (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];
    }
}

Repeat as necessary for all the other data source delegates.

Just a thought...


Well, I solved the problem by replacing the return statements of the delegate methods in my original posting from

return [self.scheduleDetailsTable cellForRowAtIndexPath:indexPath]; 

to

return [super tableView:tableView cellForRowAtIndexPath:indexPath];

And similarly for the other delegate methods. Now everything works like expected. Thanks to peterjb for using this syntax in his response to my original question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜