Ios 调整分组的UITableView的宽度
我一直在寻找这个问题的答案,但没有成功。我已经使用Ios 调整分组的UITableView的宽度,ios,objective-c,uitableview,resize,Ios,Objective C,Uitableview,Resize,我一直在寻找这个问题的答案,但没有成功。我已经使用Grouped样式创建了一个UITableView。这是一款适用于iPad的横向应用程序,我只想要左边的桌子(在0、300、20、748区域),但是设置tableView.frame=CGRectMake(0、20、300、748)没有任何作用 #import "ViewController.h" @interface ViewController () @property (strong, nonatomic) NSArray *secti
Grouped
样式创建了一个UITableView。这是一款适用于iPad的横向应用程序,我只想要左边的桌子(在0、300、20、748区域),但是设置tableView.frame=CGRectMake(0、20、300、748)
没有任何作用
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) NSArray *sections;
@end
@implementation ViewController
@synthesize sections = _sections;
- (void)viewDidLoad
{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 300, 748) style:UITableViewStyleGrouped];
tableView.frame = CGRectMake(0, 20, 300, 748);
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
NSArray *first = [NSArray arrayWithObjects:@"first", @"second", @"third", nil];
NSArray *second = [NSArray arrayWithObjects:@"fourth", @"fifth", @"sixth", nil];
self.sections = [NSArray arrayWithObjects:first, second, nil];
self.view = tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.sections count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.sections objectAtIndex:section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
cell.textLabel.text = [[self.sections objectAtIndex:[indexPath section]]objectAtIndex:[indexPath row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
tableView.frame = CGRectMake(0, 20, 300, 748);
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我正在寻找一种方法来调整表的大小,使它只有300像素宽,在左边。关于如何实现这一点有什么建议吗?假设您的
ViewController
是UIViewController
,而不是将表视图作为主视图(将为您调整大小),只需添加表视图即可
替换:
self.view = tableView;
与:
现在,表视图将保留您设置的框架
由于您希望表格视图位于左侧下方,并且可能要填充高度,因此确实应该这样做:
- (void)viewDidLoad {
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 300, self.view.frame.size.height) style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
NSArray *first = [NSArray arrayWithObjects:@"first", @"second", @"third", nil];
NSArray *second = [NSArray arrayWithObjects:@"fourth", @"fifth", @"sixth", nil];
self.sections = [NSArray arrayWithObjects:first, second, nil];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:tableView];
[tableView reloadData]; // don't reload until it's added and the data is ready
}
您是否已检查表视图上的
自动重设大小写
属性?查看默认设置为什么。此外,您可能需要检查ViewController
上的frame/autoresizingMask属性。可能是您的包含视图正在填充横向框架,而您的表视图也在这样做。您的ViewController
(坏名字)是否扩展了UIViewController
或UITableViewController
?是的,这是我刚刚制作的一个测试类(因此得名),谢谢您的快速回复,我已经把它整理好了,谢谢:)顺便说一下,它的UIViewController:D
- (void)viewDidLoad {
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 300, self.view.frame.size.height) style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
NSArray *first = [NSArray arrayWithObjects:@"first", @"second", @"third", nil];
NSArray *second = [NSArray arrayWithObjects:@"fourth", @"fifth", @"sixth", nil];
self.sections = [NSArray arrayWithObjects:first, second, nil];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:tableView];
[tableView reloadData]; // don't reload until it's added and the data is ready
}