ios - 向 TableView 中的行添加边距/填充 - objective-c

标签 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;
}

最佳答案

创建您自己的单元格,而不是使用 UITableViewCell 的默认标题和 ImageView 。制作 Storyboard,将单元格添加到 tableView,并添加 ImageView /标签/按钮或其他内容,添加约束,然后您可以拥有任何您想要的布局/定位。

您的 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;
}

关于ios - 向 TableView 中的行添加边距/填充 - objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37104155/

相关文章:

ios - 如何以编程方式将不同单元格的不同图像添加到 tableView (Swift 3)

javascript - 创建具有自动调整大小的文本区域

css - 两个 div 彼此重叠,每个 div 占据浏览器屏幕的全部高度(和宽度)

ios - 雪碧/SKScene : How to draw from top-left instead from bottom-left?

iphone - UIWebView 历史 If 语句

ios - 获取Facebook用户好友列表?

Flutter - 容器高度与子 Text.length 相同?

ios - 重新加载 UITableView 除了它的一部分

arrays - 在 swift 4.2 的 TableView 中重复数组的 n 索引

ios - 处理使用异步 API 调用填充 TableView