Ale*_*ber 5 xcode uitableview tableview ios
我在GitHub为我的问题准备了一个简单的测试项目.
使用UITableViewCellStyleSubtitle类型的单元格(在Xcode Interface Builder中称为Subtitle)时 - 由于某种原因水平线未到达屏幕的左侧:

在上面的屏幕截图中,您可以看到"空单元格"中的分隔线占据了整个宽度.
首先,我认为图标(大小:46 x 46像素)不适合细胞,并试图增加行高,但这没有帮助.
此外,我已尝试将自定义插入设置为自定义,左侧为0像素(表视图和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;
}
Run Code Online (Sandbox Code Playgroud)
更新:这个答案的源代码最终帮助了我: iOS 8 UITableView分隔符插入0不起作用

-(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];
}
}
*/
Run Code Online (Sandbox Code Playgroud)
Ash*_*kad 11
将表视图分隔符插入添加到零.不只是表视图单元格.

将此代码添加到cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//....
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
}
Run Code Online (Sandbox Code Playgroud)
与您的代码集成:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
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;
}
Run Code Online (Sandbox Code Playgroud)
查看预览:

| 归档时间: |
|
| 查看次数: |
2969 次 |
| 最近记录: |