indentationLevelForRowAtIndexPath 不缩进自定义单元格
我在我的 UITableViewController
派生类中重写了 tableView:indentationLevelForRowAtIndexPath
方法,如下所示:
I have overridden the tableView:indentationLevelForRowAtIndexPath
method in my UITableViewController
derived class as follows:
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary* item = [self.projects objectAtIndex:indexPath.row];
int indentationLevel = [[item objectForKey:@"indent"] intValue];
DLog (@"Indentation Level for Row %d : %d", indexPath.row, indentationLevel);
return indentationLevel;
}
我最初认为这不是被调用,而是操作员错误(错误,我的)并且我没有定义符号 DEBUG=1.
I initially thought that this was not being called but that was operator error (err, mine) and I hadn't defined the symbol DEBUG=1.
然而,它被调用(废话!)这是日志输出:
However, it is being called (duh me!) and this is the log output:
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 0 : 1
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 1 : 1
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 2 : 2
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 3 : 2
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 4 : 2
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 5 : 1
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 6 : 2
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 7 : 2
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 8 : 1
但是,这不会影响单元格的布局.没有缩进.
But, this is not affecting the layout of the cells. No indentation.
这是我的 itemCellForRowAtIndexPath
实现,如果这有什么不同的话:
This is my itemCellForRowAtIndexPath
implementation, if that makes any difference:
-(UITableViewCell*)tableView:(UITableView *)tableView itemCellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = @"projectItemCell";
ProjectItemTableViewCell* cell = (ProjectItemTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ProjectItemTableViewCell" owner:self options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[ProjectItemTableViewCell class]]) {
cell = (ProjectItemTableViewCell*)oneObject;
}
}
}
NSDictionary* item = [self.projects objectAtIndex:indexPath.row];
cell.projectDescLabel.text = [item objectForKey:@"name"];
cell.itemCountlabel.text = [NSString stringWithFormat:@"%d", [[item objectForKey:@"cache_count"] intValue]];
cell.itemCountlabel.backgroundColor = [UIColor colorForHex:[item objectForKey:@"color"]];
cell.indentationWidth = 20;
return cell;
}
如何缩进我在 Interface Builder 中定义的自定义 UITableViewCell
?
How do I indent a custom UITableViewCell
which I have defined in Interface Builder?
如果我将 itemCellForRowAtIndexPath
更改为使用默认 UITableViewCell
和下面的代码,那么它缩进得很好.
If I change the itemCellForRowAtIndexPath
to use a default UITableViewCell
with the code below, then it indents fine.
static NSString* cellIdentifier = @"projectItemCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
NSDictionary* item = [self.projects objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"name"];
cell.indentationWidth = 40;
return cell;
是的,似乎自定义表格单元格不会自动执行此操作?您需要覆盖表格单元格类中的 layoutSubviews
方法.有关如何执行此操作,请参阅此问题.
Yeah, it seems like custom table cells don't do this automatically? You need to override the layoutSubviews
method in the table cell class. See this question for how to do this.
这段代码对我来说非常有效(尽管如果您还为委托设置自定义高度,请小心,它们似乎相互干扰):
This code worked perfectly for me (although be careful if you are setting a custom height w/ the delegate as well, they seem to interfere with each other):
- (void)layoutSubviews
{
[super layoutSubviews];
float indentPoints = self.indentationLevel * self.indentationWidth;
self.contentView.frame = CGRectMake(
indentPoints,
self.contentView.frame.origin.y,
self.contentView.frame.size.width - indentPoints,
self.contentView.frame.size.height
);
}
针对 iOS8 及更高版本进行编辑
以上在 iOS 上确实对我有用,但在尝试自动调整单元格的高度时也会导致细微的错误.有一个更简单的解决方案:如果您为单元格启用了自动布局,只需设置 contentView 的左边距:
Edit for iOS8 and later
The above does work for me on iOS, but it causes subtle bugs when trying to autosize the height of the cell as well. There is n easier solution: If you have autolayout turned for the cell just set the left margin of the contentView:
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutMargins.left = CGFloat(self.indentationLevel) * self.indentationWidth
self.contentView.layoutIfNeeded()
}