在 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.textLabel、cell.detailTextLabel 和cell.imageView 是未触及,因此它们会提醒nil 并且不会显示。
参考:
仔细查看表格视图单元格
https://developer.apple.com/Library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1
希望对您有所帮助!