【问题标题】:How to indent only UITableViewCell content not the separator?如何仅缩进 UITableViewCell 内容而不是分隔符?
【发布时间】:2016-09-02 20:15:55
【问题描述】:

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

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

如果我用这个也一样:

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

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

cell.indentationLevel = 10;

【问题讨论】:

  • 意味着你想要分隔符原样。只是想移动内容吗??
  • 您的 tableviewcell 自定义或原生
  • 大家好。我想要缩进内容但不与边框一起。阿什米是的。原生细胞

标签: ios objective-c uitableview


【解决方案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

这是代码的截图:

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多