【发布时间】:2015-07-21 23:41:05
【问题描述】:
我已经为我在 GitHub 上的问题准备了 a simple test project。
当使用UITableViewCellStyleSubtitle 类型的单元格(在 Xcode Interface Builder 中称为 Subtitle)时 - 由于某种原因,水平线没有到达屏幕的左侧:
在上面的屏幕截图中,您可以看到“空单元格”中的分隔线很好地占据了整个宽度。
首先我认为图标(大小:46 x 46 pixels)不适合单元格并尝试增加行高,但这没有帮助。
我还尝试将 Custom Insets 设置为 Custom 并将 Left 设置为 0 像素(均用于 Table View em> 和 MyCell),但左侧仍有空隙:
这里是我TableViewController.m的源代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.items = [[NSMutableArray alloc] initWithArray:@[
@{@"title": @"Title 1", @"subtitle": @"Sub Title 1", @"icon": @"signal4.png"},
@{@"title": @"Title 2", @"subtitle": @"Sub Title 2", @"icon": @"signal1.png"},
@{@"title": @"Title 3", @"subtitle": @"Sub Title 3", @"icon": @"signal2.png"},
@{@"title": @"Title 4", @"subtitle": @"Sub Title 4", @"icon": @"signal3.png"}
]];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
NSDictionary* item = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = item[@"title"];
cell.detailTextLabel.text = item[@"subtitle"];
UIImage* icon = [UIImage imageNamed:item[@"icon"]];
[cell.imageView setImage:icon];
return cell;
}
更新: 这个答案的源代码最后帮助了我: iOS 8 UITableView separator inset 0 not working
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
/*
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Force your tableview margins (this may be a bad idea)
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
*/
【问题讨论】:
-
在 iOS 8 中,您必须添加一些代码才能这样做。检查答案
-
亚历山大,好久不见! :) 我可以说服你不要这样做吗?有插图是有美学原因的,为什么有图像时插图会改变。
标签: ios xcode uitableview tableview