我正在尝试将
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
- (instancetype)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
}
return self;
}
当你实例化视图控制器时,你会处理这个问题。
例如,要实例化一个普通的 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] 时,苹果提供的代码将负责为您构建具有请求的视图样式的表格视图。
不关注?您不必“创建新的表视图”是什么意思?您仍然需要实例化一个。
要么您已经创建了一个并且它具有您想要的样式,要么您必须实例化一个新的并为其设置属性。
tableView.style 是只读的。因此您无法更改现有样式。你必须做类似的事情:
[MyTableViewSubClass initWithFrame:aFrame style:UITableViewStyleGrouped];
您不能只更改
UITableView
的样式。所以你只有2个选择:
UITableView
,将其分组希望有帮助
您可以创建一个新的表格视图来覆盖
UITableviewController
的表格视图,如下所示:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
self.tableView = tableView;
UITableViewController 的 iOS 风格
Swift 版本
final lass CustomTableViewController: UITableViewController {
required init(customParam: String) {
super.init(style: .grouped)
}
}
只需好友分配设置表格的框架和样式..示例代码写下来。
[tableObject initWithFrame:CGRectMake(x,y,width,height) style:UITableViewStyleGrouped];