【发布时间】:2015-03-12 08:04:28
【问题描述】:
我的目标是 iOS 7 和 8,我想消除下面这个 UITableView 中图像左侧出现的边距。如果解决方案简单/优雅,我什至愿意接受仅适用于 iOS 8 的解决方案。 (在 iOS 7 消亡时,我只能忍受它的余量)。
这是我想要的样子:
我已经阅读了许多关于 Stack Overflow (such as this one) 或特定于分组 UITableViews (here 和 here) 的类似问题,但似乎无法让任何问题正常工作。
例如,如果我尝试使用 contentInset(SO 上的常见答案)解决问题:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, -16, self.tableView.contentInset.bottom, self.tableView.contentInset.right);
self.tableView.separatorInset = UIEdgeInsetsMake(0, 33, 0, 0);
}
然后我最终得到了tableview右侧的间隙:
我明白为什么会这样了,整个 tableview 只是移动了,如 Debug View Hierarchy 屏幕所示:
但是,我尝试调整 tableView 框架大小以进行补偿,但它似乎永远无法正常工作。另外,这看起来很老套;一定有更好的办法。
为了记录,这是我的 cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get a cell that we can use]
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];
cell.textLabel.numberOfLines = 0;
cell.selectedBackgroundView = selectionColor;
cell.accessoryView = [[UIImageView alloc] initWithImage:[PTStyleKit imageOfArrow]];
// Create the attributed string (text + attributes)
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:[videos objectAtIndex:indexPath.row] attributes:attributes];
// Set it in our UILabel and we are done!
[cell.textLabel setAttributedText:attributedText];
NSString* filename = [filenames objectAtIndex:indexPath.row];
if (filename == nil)
{
filename = cell.textLabel.text;
}
UIImage *imageOne = [UIImage imageNamed:[NSString stringWithFormat:@"medium-%@", filename]];
cell.imageView.image = [UIImage imageWithCGImage:imageOne.CGImage scale:imageOne.size.height/44 orientation:imageOne.imageOrientation];
return cell;
}
【问题讨论】:
-
看起来是 tableviewcell 的问题,而不是 tableview 本身的问题。您也可以发布您的
cellForRowAtIndexPath:方法吗? -
感谢您查看@Novarg,刚刚添加。
-
你也可以调试那个方法并记录
cell.imageView的位置吗? -
@Novarg 我看到 x:0.000000, y: 0.000000
NSLog(@"x: %f, y: %f", cell.imageView.frame.origin.x, cell.imageView.frame.origin.y);。是这个意思吗? -
您总是可以使用 nib 制作自定义单元格,并且比使用默认 UITableviewCell 更容易且可自定义程度更高
标签: ios swift uitableview