2013-05-10 27 views
1

我搜索了这个问题的答案,但没有成功。我使用Grouped风格创建了一个UITableView。这是一款适用于iPad的风景应用程序,我只想要左侧的桌子(在区域0,300,20,748),但是设置tableView.frame = CGRectMake(0, 20, 300, 748)什么也不做。调整分组UITableView的宽度

#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像素宽,左边。有关这可能性的任何建议?

+0

你检查你的表视图的'autoresizingMask'财产?看看它默认的是什么。另外,你可能想检查'ViewController'上的frame/autoresizingMask属性。您的包含视图可能会填充景观框架,并且您的桌面视图也可能会出现问题。 – Aaron 2013-05-10 22:05:11

+0

你的'ViewController'(坏名BTW)扩展'UIViewController'或'UITableViewController'吗? – rmaddy 2013-05-10 22:07:59

+0

是啊,这是一个测试类,我刚刚(因此得名),并感谢所有的快速回复,我已经整理出来,虽然谢谢:) – bruceyyy 2013-05-10 22:30:32

回答

0

假设你的ViewControllerUIViewController,而不是使表视图的主视图(这将为您调整大小),只需添加表视图。

替换:

self.view = tableView; 

有:

[self.view addSubview: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 
} 
+0

感谢您的回应!帮助:) – bruceyyy 2013-05-10 22:29:08