1

我一直在寻找这个问题的答案,但没有成功。我使用该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 像素宽。关于这怎么可能的任何建议?

4

1 回答 1

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
}
于 2013-05-10T22:12:39.287 回答