为什么我有这种奇怪的间距

时间:2012-10-24 08:39:14

标签: objective-c ios xcode uitableview

我无法摆脱第0行和第1行之间的这种奇怪间距。 我希望第0行和第1行之间的间距与第1行和第2行之间的间距相同。

enter image description here

[self.tableView setBackgroundColor:TABLE_VIEW_BACKGROUND_COLOR];

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];

UIImageView *headerBackgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addres_box"]];
[headerBackgroundImageView setFrame:CGRectMake(headerView.frame.origin.x + 10, headerView.frame.origin.y + 10, headerView.frame.size.width - 20, 160)];
[headerView addSubview:headerBackgroundImageView];

UILabel *headerTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(headerBackgroundImageView.frame.origin.x + 10, headerBackgroundImageView.frame.origin.y + 10, headerBackgroundImageView.frame.size.width - 20, 20)];
[headerTextLabel setBackgroundColor:[UIColor clearColor]];
[headerTextLabel setFont:[UIFont boldSystemFontOfSize:headerTextLabel.font.pointSize]];
[headerTextLabel setTextColor:[UIColor whiteColor]];
[headerTextLabel setLineBreakMode:UILineBreakModeTailTruncation];
[headerTextLabel setNumberOfLines:0];

NSString *headerTextString = @"0";
NSString *detailTextString = @"1\n2";

[headerTextLabel setText:headerTextString];
[headerView addSubview:headerTextLabel];
_headerTextLabel = headerTextLabel;

UILabel *headerDetailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(headerTextLabel.frame.origin.x, headerTextLabel.frame.origin.y+20, headerTextLabel.frame.size.width, 140)];
[headerDetailTextLabel setBackgroundColor:[UIColor clearColor]];
[headerDetailTextLabel setTextColor:[UIColor whiteColor]];
[headerDetailTextLabel setLineBreakMode:UILineBreakModeTailTruncation];
[headerDetailTextLabel setNumberOfLines:0];

[headerDetailTextLabel setText:detailTextString];
[headerView addSubview:headerDetailTextLabel];
_headerDetailTextLabel = headerDetailTextLabel;

[self.tableView setTableHeaderView:headerView];
[self.tableView setSeparatorColor:TABLE_VIEW_SEPARATOR_COLOR];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

我希望有人可以帮助我!

2 个答案:

答案 0 :(得分:1)

也许您的headerDetailTextLabel垂直居中,尝试添加更多行,例如“1 \ n2 \ n3 \ n4”并查看结果。

答案 1 :(得分:0)

感谢提示,我通过添加“sizeToFit”来解决它,现在每件事情看起来都不错

[headerDetailTextLabel setText:detailTextString];
[headerDetailTextLabel sizeToFit]; //this line fixed the spacing problem
[headerView addSubview:headerDetailTextLabel];