如果我使用此代码从左缩进,则所有内容都缩进(分隔符+内容)

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


如果我使用这个也一样:

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


这也无济于事,因为它不会移动图像。

cell.indentationLevel = 10;

最佳答案

您可以使用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


这是该代码的屏幕截图:

ios - 如何只缩进UITableViewCell内容而不缩进分隔符?-LMLPHP

您可以根据需要在“ layoutSubviews”中修改代码。

希望它能对您有所帮助。

关于ios - 如何只缩进UITableViewCell内容而不缩进分隔符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39289632/

10-14 21:00
查看更多