问题描述
- tableView占满整个屏幕
- 在app中需要用到一个小 tableView ,我设置了尺寸,但是运行之后 tableview 占了整个屏幕。
-(void)viewDidLoad{ UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0 0.0 20.0 60.0) style:UITableViewStylePlain]; self.view = tableView; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *MyCellIdentifier = @""MyCellIdentifier"";UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];}return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 5; }
解决方案
self.view = tableView;
改成
[self.view addSubview:tableView];
解决方案二:
self.view = tableView;
你这样写是把当前控制器的视图设置为了tableView,
应该使用[self.view addSubview:tableView];
这样是把tableView添加在当前视图上面
时间: 2024-08-01 17:56:08