我试图让一些文本对齐到节标题的左侧,而不同的文本对齐到同一标题的右侧。
在使用工具栏时,我使用了一个灵活的按钮来向左和向右插入其他两个按钮,我想知道章节标题是否有类似的东西?
更多一点的背景是,我在我的表格 View 中有一个不同的部分,用于在不同的日期输入的每条信息。在部分标题的左侧,我想要输入日期(11 月 20 日,星期三),在右侧,我想要那是多少天前(20 天前)。
这是我目前使用的代码:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
}
else {
// This gets the day the data was entered
NSDate * date = [_sectionDates objectAtIndex:(section-1)];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE"];
NSString * dateDay = [formatter stringFromDate:date];
[formatter setDateFormat:@"MMM"];
NSString * dateMonth = [formatter stringFromDate:date];
NSDateComponents * components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSInteger dateOfDay = [components day];
NSTimeInterval diff = [[NSDate date] timeIntervalSinceDate:date];
NSInteger days = diff / (60.0 * 60.0 * 24);
NSString * dateScript;
if (dateOfDay < 21 && dateOfDay > 4) {
dateScript = @"th";
}
else if (dateOfDay % 10 == 1) {
dateScript = @"st";
}
else if (dateOfDay % 10 == 2) {
dateScript = @"nd";
}
else if (dateOfDay % 10 == 3) {
dateScript = @"rd";
}
else {
dateScript = @"th";
}
NSString * header;
if (days < 2) {
header = [NSString stringWithFormat:@"%@ %i%@ %@ - %@", dateDay, dateOfDay, dateScript, dateMonth, days == 0 ? bToday : bYesterday];
}
else {
header = [NSString stringWithFormat:@"%@ %i%@ %@ - %i %@", dateDay, dateOfDay, dateScript, dateMonth, days, bDaysAgo];
}
return header;
}
}
此时输出如下:
|12 月 4 日星期三 - 今天________|
我希望它是这样的:
|12 月 4 日星期三_________今天|
最佳答案
你不能用 titleForHeaderInSection
来做到这一点。您需要改为实现 viewForHeaderInSection
。返回一个带有两个标签的 View ——一个在左边,一个在右边。当然,如果您想要的话,您的 View 还需要复制标准表格部分标题的默认外观。
关于ios - 是否可以在表格 View 的部分标题中将文本左右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366598/