我有一个带有一些表格的应用程序。
在此表中有两个标签 - 带标题的 textLabel
和带日期的 detailTextLabel
。但当标题很长时,它会显示在 detailTextLabel
上。
我该如何解决这个问题?
附注
我尝试以这种方式重写 layoutSubviews
方法:
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.bounds.size;
CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height);
self.textLabel.frame = frame;
self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}
但是没用
最佳答案
首先我创建空单元格。然后添加 UILabel 作为带有标签的 subview 。并更改UILabel的框架
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Recent Dream";
UITableViewCell *cell;// = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(@"new cell");
UILabel *textLabel = [[UILabel alloc] init];
textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48);
textLabel.backgroundColor = [UIColor clearColor];
textLabel.font = [UIFont boldSystemFontOfSize:16];
[textLabel setTag:1];
[cell.contentView addSubview:textLabel];
}
// Configure the cell...
Dream *tempDream = [self.dreams objectAtIndex:indexPath.row];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]];
cell.contentView.backgroundColor = background;
UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1];
textLabel.text = tempDream.title;
cell.textLabel.text = @"";
cell.detailTextLabel.text = stringFromDate;
return cell;
}
关于ios - 让我们缩短 cell.textLabel 的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307046/