gpt4 book ai didi

ios - UITableViewCell 中 UITextLabel 的大小

转载 作者:行者123 更新时间:2023-11-29 12:56:53 26 4
gpt4 key购买 nike

我通过重写 -(void)layoutSubviews 方法对 UITableViewCell 进行了子类化:

-(void)layoutSubviews {
[super layoutSubviews]; //The default implementation of the layoutSubviews

CGRect textLabelFrame = self.textLabel.frame;
textLabelFrame.size.width = 180;
self.textLabel.frame = textLabelFrame;
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.textLabel.adjustsFontSizeToFitWidth = YES;

CGRect detailTextLabelFrame = self.detailTextLabel.frame;
detailTextLabelFrame.size.width = 30;
self.detailTextLabel.frame = detailTextLabelFrame;
self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
self.detailTextLabel.adjustsFontSizeToFitWidth = YES;

[self sizeToFit];

}

在单元格内,我还在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中添加了一个 UIStepper subview :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// code omitted
UITableViewCell *cell;
if(indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:OrderCellIdentifier forIndexPath:indexPath];
if ( cell == nil ) {
cell = [[GinkgoDeliveryOrderCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:OrderCellIdentifier];
UIStepper * stepper = [[UIStepper alloc] initWithFrame:CGRectMake(113, 7, 94, 29)];
[cell addSubview:stepper];
}
// code omitted
}

但是,当textLabel 的文本太长时,它似乎挤掉了detailTextLabel。我想知道为什么会这样,因为我已经为这些 subview 指定了框架。 enter image description here

提前致谢!

最佳答案

好的,我已经通过为框架手动设置CGRect解决了这个问题:

-(void)layoutSubviews {
[super layoutSubviews]; //The default implementation of the layoutSubviews

self.textLabel.frame = CGRectMake(15, 11, 180, 21);
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.textLabel.adjustsFontSizeToFitWidth = YES;

CGRect detailTextLabelFrame = CGRectMake(280, 11, 40, 21);
self.detailTextLabel.frame = detailTextLabelFrame;
self.detailTextLabel.textAlignment = NSTextAlignmentCenter;

[self sizeToFit];

}

感谢@Bamsworld。

关于ios - UITableViewCell 中 UITextLabel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20849011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com