ios - 是否可以在表格 View 的部分标题中将文本左右对齐?

标签 ios objective-c uitableview sections

我试图让一些文本对齐到节标题的左侧,而不同的文本对齐到同一标题的右侧。

在使用工具栏时,我使用了一个灵活的按钮来向左和向右插入其他两个按钮,我想知道章节标题是否有类似的东西?

更多一点的背景是,我在我的表格 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/

相关文章:

javascript - window.location 的使用在 iPad 上不起作用

ios - 如何检查用户是否在 4 秒内没有写任何东西?

ios - undefined symbol : _OBJC_CLASS_$_WKInterfaceController error after updating Xcode to version 11. 1

iphone - 核心数据`deleteObject:`问题!

c++ - Cocoa-track 的菜单/将选项注入(inject) itunes

ios - 如何在具有索引行单元格的同一 View Controller 中加载两个 TableView ?

iphone - 滚动表与节标题重叠

ios - Apple Pay 在 PKPaymentAuthorizationControllerExportedObject authorizationDidFinishWithError 上崩溃

ios - 从 TableVC 解析到其他 viewController 中的 textView swift

ios - 取消对 Alamofire 图片的请求