我正在尝试将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;
}
不关注?您不必"制作新的表视图"是什么意思?您仍然需要实例化一个。
要么你已经创建了一个并且它具有你想要的样式,要么你必须实例化一个新的并在其上设置属性。
tableView.style 是只读的。因此,您无法更改现有样式。您将必须执行以下操作:
[MyTableViewSubClass initWithFrame:aFrame style:UITableViewStyleGrouped];
您不能只是更改UITableView
的样式。所以你只有2个选择:
- 制作另一个分组的
UITableView
- 使用自定义单元格
希望对你有帮助
实例化视图控制器时,您将处理此问题。
例如,要实例化一个普通的UITableViewController,你需要执行以下操作。
UITableViewController *tblCtr = [[UITableViewController alloc]initWithStyle:UITableViewStyleGrouped];
因此,如果你已经扩展了UITableViewController,那么你的init代码应该处理这个问题。
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 提供的代码将负责使用请求的视图样式为您构建表视图。
创建一个新的表视图来覆盖UITableviewController
的表视图,如下所示:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
self.tableView = tableView;
计来分配设置表格的框架和样式..示例代码写下来。
[tableObject initWithFrame:CGRectMake(x,y,width,height) style:UITableViewStyleGrouped];