5

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

iOS 图像

4

2 回答 2

9

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

indentationLevel
indentationWidth
shouldIndentWhileEditing
separatorInset

因此,创建自己的自定义UITableViewCell是克服它的最佳方法。
根据 Apple的说法,有两种好方法可以做到这一点:

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

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

解决方案:

由于您不喜欢 subclassing 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

希望这有帮助!

于 2013-11-22T08:41:17.820 回答
6

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

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

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

- (void) layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake( 0, 0, 50, 55 );
}
于 2013-09-30T21:17:11.430 回答