2013-05-10 4 views
1

Я искал, без всякого успеха, ответ на этот вопрос. Я создал UITableView, используя стиль Grouped. Это приложение для iPad для iPad, и мне нужна только таблица слева (в области 0, 300, 20, 748), однако установка tableView.frame = CGRectMake(0, 20, 300, 748) ничего не делает.Настроить ширину Grouped 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' свойство на ваше представлении таблицы? Посмотрите, на что он по умолчанию. Кроме того, вы можете проверить свойство frame/autoresizingMask на 'ViewController'. Возможно, ваш сложенный вид заполняет рамку ландшафта, и ваш вид таблицы соответствует примеру. – Aaron

+0

Расширяет ли ваш 'ViewController' (плохое имя BTW)' UIViewController' или 'UITableViewController'? – rmaddy

+0

Да, это был класс испытаний, который я только что сделал (отсюда и имя), и спасибо за все быстрые ответы, я разобрал его, хотя спасибо :) – bruceyyy

ответ

0

Предполагая, что ваш ViewController является UIViewController, вместо того, чтобы сделать вид таблицы основным видом (который будет изменен для вас), просто добавьте представление таблицы.

Заменить:

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

Смежные вопросы