2013-05-10 24 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