我正在开发一个应用程序,它要求部分的标题应该在右边而不是默认的左边。
我搜索了很多极客建议实现:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static UIView *headerView;
if (headerView != nil)
return headerView;
NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil);
// set the container width to a known value so that we can center a label in it
// it will get resized by the tableview since we set autoresizeflags
float headerWidth = 150.0f;
float padding = 10.0f; // an arbitrary amount to center the label in the container
headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// create the label centered in the container, then set the appropriate autoresize mask
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, headerWidth - 2.0f * padding, 44.0f)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
headerLabel.textAlignment = UITextAlignmentRight;
headerLabel.text = headerText;
[headerView addSubview:headerLabel];
return headerView;
}
但是,当我尝试增加页眉 View 框架的 x 坐标时,它不会影响 View 的位置。并且它始终位于表格 View 的中心。
我需要标题在右边。
最佳答案
这对我有用,但是可以根据您的需要调整框架的坐标
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] init];
label.text=@"header title";
label.backgroundColor=[UIColor clearColor];
label.textAlignment = NSTextAlignmentRight;
return label;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
关于iphone - 右侧的 UITableView 部分标题而不是左侧的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672231/