2016-09-02 1 views
0

私は左からインデントするために、このコードを使用する場合は、すべてが(セパレータ+コンテンツ)インデントされセパレータではなく、UITableViewCellのコンテンツだけをインデントする方法は?

table.contentInset = UIEdgeInsetsMake(0, 20, 0, 0); 

同じ私はこの使用している場合:

cell.layoutMargins = UIEdgeInsetsMake(0, 20, 0, 0); 

を、それはdoesnのため、これは、どちらも助けません。イメージを移動しないでください。

cell.indentationLevel = 10; 
+0

セパレータをそのまま使用したいと思います。内容を正しく移動したいですか? – ashmi123

+0

テーブルビューセルのカスタムまたはネイティブ –

+0

こんにちは。私はインデントのコンテンツを必要としますが、境界線と一緒にはしません。 ashmiはい。ネイティブセル – luky

答えて

1

あなたが望む結果を得るためにCustomTableCellを使用することができます。

まず、のViewControllerに:

#import "ViewController.h" 
#import "MyTableCell.h" 

@interface ViewController() 

@property UITableView *tableView; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; 
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone; 
    _tableView.backgroundColor = [UIColor grayColor]; 
    _tableView.dataSource = self; 
    _tableView.delegate = self; 
    [self.view addSubview:_tableView]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 20; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    MyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"]; 
    if (nil == cell) { 
     cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"]; 
    } 
    //Update data for cell 

    return cell; 
} 

@end 

これはMyTableCell.hです:

#import "MyTableCell.h" 

@interface MyTableCell() 

@property UIView *containerView; 
@property UIView *separationLine; 

@end 

@implementation MyTableCell 

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 

    _containerView = [[UIView alloc] init]; 
    _containerView.backgroundColor = [UIColor redColor]; 
    [self addSubview:_containerView]; 

    _separationLine = [[UIView alloc] init]; 
    _separationLine.backgroundColor = [UIColor blackColor]; 
    [self addSubview:_separationLine]; 

    return self; 
} 

-(void)layoutSubviews{ 
    _containerView.frame = CGRectMake(20, 0, self.frame.size.width-20, self.frame.size.height-1); 
    _separationLine.frame = CGRectMake(10, self.frame.size.height-1, self.frame.size.width-10, 1); 
} 

@end 

そして、これコードのスクリーンショット:

enter image description here

"layoutSubviews"でコードを変更することができます。

お手数ですがお手伝いします。

関連する問題