UITableView作为tableHeaderView的另一个UITableView
我遇到以下问题:我想使用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替换标题视图,然后一切按预期工作(即父类中的表视图正确实现)。我可能只是错过了一些显而易见的东西,但我现在无法弄清楚。如果有人能够发现错误或者可以提供一些建议,我会非常感谢。
不是没有,但你真的需要创建一个整体
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];
}
}
必要时重复所有其他数据源的代表。
只是一个想法...
嗯,我解决了这个问题由
return [self.scheduleDetailsTable cellForRowAtIndexPath:indexPath];
替换我原来张贴的委托方法的返回语句
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
而且对于其他代表方法也是如此。现在一切都按预期工作。感谢peterjb在回答我的原始问题时使用了这种语法。
我想做一些非常类似于EKEventViewController的东西。但它不会编译,因为EKEventViewController没有在其接口中定义的委托方法。有任何想法吗? – roocell 2012-09-21 17:59:53
感谢您的回复。实际上,你给出的单个UITableViewCell选项确实是最好的选择。但是,唯一的问题是,当这个单元格被选中(或者可能存在?)时,似乎不可能触发某个操作(即推送新视图)。另外,您给出的第二个选项对于我的情况并不适合,因为顶部单元格需要是普通单元格,而底部表格视图样式需要分组。 – MJD 2011-03-25 07:59:25
只是想到了自己......将UIButton作为子视图添加到单个UITableViewCell中以便能够触发某些操作是否是个好主意? – MJD 2011-03-25 08:06:37
@MJD很高兴你解决了你的问题。 FWIW,在整个表格视图单元格中放置一个按钮并跟踪点击可能是可以的。如果您在选择单元格以匹配普通表格视图时想要使用hilight,则必须自己调用[tableViewCell setSelected:YES animated:NO]以匹配预期的行为。 (由于某些原因,当你在表格视图中选择一个单元格时,操作系统不会使用内置的分析...) 但是,它确实开始有点黑客 - 你可能会更好如果你需要两种不同的风格,请按照你的方式做... – peterjb 2011-03-26 02:43:35