Ios 如何填充UITableView节标题?
如何向UITableView单元格中的节标题添加一些填充 我有这个: 我有一个NSDictionary,它的键值包含一个时间字符串。我将键值存储在NSArray中 我正在使用标题ForHeaderInSection而不是viewForHeaderInSectionIos 如何填充UITableView节标题?,ios,objective-c,xcode,Ios,Objective C,Xcode,如何向UITableView单元格中的节标题添加一些填充 我有这个: 我有一个NSDictionary,它的键值包含一个时间字符串。我将键值存储在NSArray中 我正在使用标题ForHeaderInSection而不是viewForHeaderInSection - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [schedul
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [schedule objectAtIndex:section];
}
但是,节标题一直向左对齐。我可以使用UIView for viewForHeaderInSection将其添加到我喜欢的位置,但是对于方法标题ForHeaderInSection有什么方法可以这样做吗?最好使用tableView:viewForHeaderInSection
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
UILabel *lblHeadertitle = [[UILabel alloc] initWithFrame:CGRectMake(YOUR_LEFT_PADDING, YOUR_TOP_PADDING, 200, 20)];
//customize lblHeadertitle
lblHeadertitle.text = [schedule objectAtIndex:section];
[viewHeader addSubview:lblHeadertitle];
return viewHeader;
}
根据此处给出的答案: 您的高度
viewsheader
=30,lblHeadertitle
=20
在代码处:
UILabel *lblHeadertitle = [[UILabel alloc] initWithFrame:CGRectMake(YOUR_LEFT_PADDING, YOUR_TOP_PADDING, 200, 20)];
设置YOUR\u TOP\u PADDING=5
,因为lblHeadertitle
是viewHeader
的子视图,并让它在viewHeader
中居中
希望能有所帮助。我无法轻松使用标题ForHeaderInSection,因此我决定使用带有viewForHeaderInSection的自定义单元格。我认为最简单的解决方案是将标签嵌套在视图中。请参见下面的Swift 2答案:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [schedule objectAtIndex:section];
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// Create the header view
let headerView = UIView.init(frame: CGRectMake(0, 0, self.view.frame.size.width, 30))
headerView.backgroundColor = UIColor.lightGrayColor()
// Create the Label
let label: UILabel? = UILabel(frame: CGRectMake(20, 0, 120, 30))
label!.textAlignment = .Left
label!.text = "Your Section Title"
// Add the label to your headerview
headerView.addSubview(label)
return headerView
}
首先,您的文本上有一个缩进为20的页眉视图:-D但是我如何使用viewForHeaderInSection显示不同的页眉标题呢?您可以设置lblHeadertitle.text=[schedule objectAtIndex:section];见更新的答案不确定原因,但章节标题已消失。我尝试了多个填充以避免无效。别忘了为标题部分设置高度,请参阅更新的答案由于某些原因,部分标题文本不对齐。不管我怎么做,它总是在同一个错误的位置。请参阅链接:谢谢,但没有帮助,您的建议导致章节标题文本完全消失