【问题标题】:iOS7 tableview cell.imageview extra padding?iOS7 tableview cell.imageview 额外填充?
【发布时间】:2013-10-06 20:14:58
【问题描述】:

我有一个在 iOS 6 中完美呈现的 tableview 并且已经这样做了多年。在 iO7 中,在 cell.imageview 的任一侧的同一 tableview 中,它在下面显示的每个图像的任一侧添加了一些额外的填充约 5mm,从而将我的 cell.textLabel.text 进一步向右移动。我将如何删除这个我似乎无法在任何地方找到这个问题的答案?

【问题讨论】:

  • 要完全控制渲染,您需要放弃内置的单元格样式(例如 UITableViewCellStyleDefault)并滚动您自己的布局。
  • 感谢@CSmith,所以 customCell 是解决这个问题的唯一方法吗?

标签: uitableview ios7


【解决方案1】:

我可能遇到了同样的问题,唯一对我有用的是设置图像框架:

cell.imageView.frame = CGRectMake( 0, 0, 50, 55 );

如果您要对单元格进行子类化,最好这样做:

- (void) layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake( 0, 0, 50, 55 );
}

【讨论】:

  • 非常感谢,但设置框架对我来说仍然没有任何区别。我的表格中的图像两侧仍然有填充物:-(。我想我可以对单元格进行子类化,但看起来就像一把锤子敲碎了坚果!
【解决方案2】:

在 iOS7 中,UITableViewCell 的预定义属性 imageView 默认向右缩进 15pt
这与以下UITableViewCell 属性无关

indentationLevel
indentationWidth
shouldIndentWhileEditing
separatorInset

因此,创建自己的自定义 UITableViewCell 是克服它的最佳方法。
According to Apple,有两种好方法可以做到:

如果您希望单元格具有不同的内容组件并将它们布置在不同的位置,或者如果您希望单元格具有不同的行为特征,您有两种选择:

  • 添加子视图到单元格的内容视图。
  • 创建 UITableViewCell 的自定义子类

解决办法:

由于您不喜欢子类化 UITableViewCell,因此添加自定义子视图是您的选择。
只需创建自己的图像视图和文本标签,然后通过代码或故事板添加它们。例如

//caution: simplied example
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get the cell object
    static NSString *CellIdentifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    //create your own labels and image view object, specify the frame
    UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)];
    [cell.contentView addSubview:mainLabel];

    UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 220.0, 25.0)];
    [cell.contentView addSubview:secondLabel];
    
    UIImageView *photo = [[UIImageView alloc] initWithFrame:CGRectMake(225.0, 0.0, 80.0, 45.0)];
    [cell.contentView addSubview:photo];
    
    //assign content
    mainLabel.text = @"myMainTitle";
    secondLabel.text = @"mySecondaryTitle";
    photo.image = [UIImage imageNamed:@"myImage.png"];
    return cell;
}

请注意,由于预定义的UITableViewCell 内容属性:cell.textLabelcell.detailTextLabelcell.imageView未触及,因此它们会提醒nil 并且不会显示。


参考:

仔细查看表格视图单元格 https://developer.apple.com/Library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2015-11-07
    • 2012-06-25
    相关资源
    最近更新 更多