ahe*_*ang 22 iphone objective-c uitableview
我希望我的UITableViewCell中的文本有点向右.换句话说,我想要一个x偏移量.这甚至可能吗?我是否因此而需要创建自定义单元格?
dem*_*ten 109
你可以使用cell.indentationLevel
,如果需要,cell.indentationWidth
而不是自定义UITableViewCell.
iOS*_*ter 31
你可以试试这个:
[cell setIndentationLevel:SOME_NUMBER];
[cell setIndentationWidth:SOME_OTHER_NUMBER];
Run Code Online (Sandbox Code Playgroud)
XJo*_*nes 11
一个简单的解决方案是您可以更改textLabel的框架.
CGRect textLabelFrame = cell.textLabel.frame;
textLabelFrame.origin.x += xOffset;
textLabelFrame.size.width -= xOffset;
cell.textLabel.frame = textLabelFrame;
Run Code Online (Sandbox Code Playgroud)
我也是通过创建一个支持类似于UIButton的edgeInsets的自定义UILabel来完成的.这是一个更好的解决方案b/c您可以将标签布局为正确的尺寸,但如果您有简单的需求,上述方法将起作用.
[编辑1/2:修复错字与CGRect]
[编辑3:固定拼写设置修改框架]
[编辑4:需要一个简单的子类]
Mea culpa.我错了,你可以做到这一点tableView:cellForRowAtIndexPath
.在tableView委托/数据源有机会自定义单元格之后,UITableViewCell布局发生.我测试了下面的实现,它的工作原理.
按我上面说的,但创建一个(简单)的子类UITableViewCell
,增加了xOffset
在layoutSubviews
.如果这样做,您还可以添加xOffset
可以设置的属性tableView:cellForRowAtIndexPath:
.
@implementation XOffsetCell
// assumes property xOffset is defined and synthesized
- (void) layoutSubviews
{
[super layoutSubviews];
CGRect textLabelFrame = cell.textLabel.frame;
textLabelFrame.origin.x += self.xOffset;
textLabelFrame.size.width -= self.xOffset;
cell.textLabel.frame = textLabelFrame;
}
@end
Run Code Online (Sandbox Code Playgroud)
该建议增加一个自定义的解决方案UILabel
来cell.contentView
也是一个不错的解决方案.我看到你的评论它掩盖了内置,textLabel
但这就是重点.您不再使用内置标签,而是使用自定义标签.
我不是为了投票,而是想表明@iPhone怪物提供的代码应该是什么样子.他的解决方案是有效的选择.如果您像往常一样将标签添加到单元格中,if (cell == nil)
您将不断向出列单元格添加标签.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 10.0f, 150.0f, 20.0f)];
lbl.tag = OffsetLabelTag; // define this as a constant
[cell.contentView addSubview:lbl];
[lbl release];
}
UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:OffsetLabelTag];
[lbl setText:@"test text"];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用自动布局做到这一点:
UITableViewCell *cell = [UITableViewCell new];
cell.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(indentation)-[textLabel]-(indentation)-|"
options:0
metrics:@{@"indentation": @35}
views:@{@"textLabel": cell.textLabel}]];
Run Code Online (Sandbox Code Playgroud)
或使用Parus lib:
[cell.contentView addConstraints:(PVVFL(@"H:|-(indentation)-[textLabel]-(indentation)-|")
.withViews(@{@"textLabel": textLabel})
.metrics(@{@"indentation": @35}).asArray)];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35790 次 |
最近记录: |