在TableView中向行添加margin / pading - Objective C

时间:2016-05-08 19:51:59

标签: ios uitableview height row cell

我是iOS开发的新手,我想要尝试一些我认为应该很容易的东西的时间。我试图增加我的行的高度,这有效,但问题是图像的大小也增加了。 所以我正在寻找一个解决方案,比如在我的行中添加边距/填充,图像的大小仍然相同,而不是像左边的照片一样?正确的照片是我正在尝试解释的一个很好的例子。

enter image description here

有人为此提供了一个很好的代码示例吗?谢谢! :)

TableView代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
    Personen *myPersoon = self.personen[indexPath.row];

    // Configure the cell...
    cell.imageView.image = [UIImage imageNamed:myPersoon.foto];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",
                           myPersoon.naam, myPersoon.voornaam];

    return cell;
}

1 个答案:

答案 0 :(得分:1)

不使用UITableViewCell的默认标题和图像视图,而是创建自己的单元格。创建一个故事板,向tableView添加一个单元格,添加图像视图/标签/按钮等,添加约束,你可以拥有你想拥有的任何布局/定位。

您的表格视图非常简单,您不想自己创建?或者,您仍然可以将其子类化并使用layoutSubviews:来改变位置:

- (void)layoutSubviews {

    [super layoutSubviews];

    CGRect frame = self.imageView.frame;
    frame.origin.y = (self.frame.size.height - kImageViewDiameter ) / 2.0f;
    frame.size.height = kImageViewDiameter;
    frame.size.width = kImageViewDiameter;
    self.imageView.frame = frame;
}