我试图将UITableViewController
的样式更改为已分组。我知道你可以在创建一个新的表视图时执行此操作,但我有一个扩展UITableViewController
的类,所以我不需要创建一个新的表视图。这是我的代码:
#import "DetailViewController.h"
#import "NSArray-NestedArrays.h"
@implementation DetailViewController
@synthesize steak, sectionNames, rowControllers, rowKeys, rowLabels;
- (void)viewDidLoad {
sectionNames = [[NSArray alloc] initWithObjects:[NSNull null], NSLocalizedString(@"General", @"General"), nil];
rowLabels = [[NSArray alloc] initWithObjects:
[NSArray arrayWithObjects:NSLocalizedString(@"Steak Name", @"Steak Name"), nil],
[NSArray arrayWithObjects:NSLocalizedString(@"Steak Wellness", @"Steak Wellness"), NSLocalizedString(@"Steak Type", @"Steak Type"), NSLocalizedString(@"Other", @"Other"), nil]
, nil];
rowKeys = [[NSArray alloc] initWithObjects:
[NSArray arrayWithObjects:@"steakName", nil],
[NSArray arrayWithObjects:@"steakWellness", @"steakType", @"other", nil]
, nil];
// TODO: Populate row controllers array
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sectionNames count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id theTitle = [sectionNames objectAtIndex:section];
if ([theTitle isKindOfClass:[NSNull class]]) {
return nil;
}
return theTitle;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rowLabels countOfNestedArray:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SteakCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
NSString *rowKey = [rowKeys nestedObjectAtIndexPath:indexPath];
NSString *rowLabel = [rowLabels nestedObjectAtIndexPath:indexPath];
cell.detailTextLabel.text = rowKey;
cell.textLabel.text = rowLabel;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// TODO: Push editing controller onto the stack
}
@end
答案 0 :(得分:3)
- (instancetype)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
}
return self;
}
答案 1 :(得分:2)
没有关注?你是什么意思,你不必“创建一个新的表视图”?你仍然需要实例化一个。
您已经创建了一个并且它具有您想要的样式,或者您必须实例化一个新的并在其上设置属性。
tableView.style是READONLY。所以你不能改变现有的风格。你将不得不做类似的事情:
[MyTableViewSubClass initWithFrame:aFrame style:UITableViewStyleGrouped];
答案 2 :(得分:2)
您不能只改变UITableView
的风格。所以你只有两个选择:
UITableView
希望有所帮助
答案 3 :(得分:1)
您可以创建一个新的tableview来覆盖UITableviewController
的tableview,如下所示:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
self.tableView = tableView;
答案 4 :(得分:0)
在实例化视图控制器时,您将会处理此问题。
例如,要实例化一个普通的UITableViewController,你可以执行以下操作。
UITableViewController *tblCtr = [[UITableViewController alloc]initWithStyle:UITableViewStyleGrouped];
因此,如果您已经扩展了UITableViewController,那么您的初始化代码应该处理这个问题。
MyCustomTableViewController *mctvc = [[MyCustomTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
要实现此目的,您需要在.m文件中实现此方法。下面是您的标头和实现文件应包含的实例化示例。
标题
@interface MyCustomTableViewController : UITableViewController
{
-(id)initWithStyle:(UITableViewStyle)style;
}
实施
@implementation MyCustomTableViewController
-(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if(self)
{
...
return self;
}
return nil;
}
@end
当您调用[super initWithStyle:style]时,apple提供的代码将负责使用请求的视图样式为您构建tableview。
答案 5 :(得分:0)
[tableObject initWithFrame:CGRectMake(x,y,width,height) style:UITableViewStyleGrouped];