Xet*_*ius 19 iphone uitableview
我已tableView:indentationLevelForRowAtIndexPath在我的UITableViewController派生类中重写了该方法,如下所示:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
我最初认为这没有被调用,但这是操作员错误(错误,我的),我没有定义符号DEBUG = 1.
然而,它被称为(对我!),这是日志输出:
-[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
Run Code Online (Sandbox Code Playgroud)
但是,这不会影响细胞的布局.没有缩进.
这是我的itemCellForRowAtIndexPath实现,如果这有任何区别:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
如何缩进UITableViewCell我在Interface Builder中定义的自定义?
如果我更改itemCellForRowAtIndexPath为使用UITableViewCell下面的代码的默认值,那么它缩进很好.
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;
Run Code Online (Sandbox Code Playgroud)
Nat*_*han 46
是的,似乎自定义表格单元格不会自动执行此操作?您需要覆盖layoutSubviews表格单元格类中的方法.有关如何执行此操作,请参阅此问题.
这段代码对我来说非常合适(虽然如果你设置一个自定义高度w /委托也要小心,它们似乎相互干扰):
- (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
);
}
Run Code Online (Sandbox Code Playgroud)
以上在iOS上对我有用,但在尝试自动调整单元格的高度时会产生微妙的错误.有一个更简单的解决方案:如果您为单元格设置了autolayout,则只需设置contentView的左边距:
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutMargins.left = CGFloat(self.indentationLevel) * self.indentationWidth
self.contentView.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24416 次 |
| 最近记录: |