TableViewCellContentView比iPad上的TableView窄,而TableView是由代码创建的

时间:2016-04-06 09:34:46

标签: ios objective-c uitableview ipad

我希望TableViewCellContentView与TableView具有相同的宽度,以下是代码:

#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong) UITableView* mainTableView;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)-20)];
  _mainTableView.delegate = self;
  _mainTableView.dataSource = self;
  [self.view addSubview:_mainTableView];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SimpleTableIdentifier];
}
cell.textLabel.text = @"111fewfwFwfrg";
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_today_arrow.png"]];
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    //(lldb) po cell
    //<UITableViewCell: 0x13dd22bf0; frame = (0 220; 768 44); text = '111fewfwFwfrg'; autoresize = W; layer = <CALayer: 0x13dd1cf30>>

    //(lldb) po cell.contentView
    //<UITableViewCellContentView: 0x13c6521a0; frame = (0 0; 711 43.5); opaque = NO; gestureRecognizers = <NSArray: 0x13c6594b0>; layer = <CALayer: 0x13c6546a0>>
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
@end

这里是图片:  http://i.stack.imgur.com/QK6a8.jpg

lldb表示TableViewCellContentView的帧是(0 0; 711 43.5),x值是0所以TableViewCell中可能有填充,但我找不到任何设置它的方法。顺便说一句,它在iPhone上运行良好。

任何人都可以帮我解决问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

在viewdidload中尝试:

[self.tableView setContentInset:UIEdgeInsetsZero];