【问题标题】:Is it possible align text to both the left and right in a section heading in a tableview?是否可以在表格视图的部分标题中将文本左右对齐?
【发布时间】:2013-12-04 04:03:46
【问题描述】:

我正在尝试将部分文本对齐到部分标题的左侧,并将不同的文本对齐到同一标题的右侧。

在使用工具栏时,我使用了一个灵活的按钮来左右推动其他两个按钮,我想知道部分标题是否有类似的东西?

更多背景知识是,我的表格视图中有一个不同的部分,用于在不同的日子输入的每条信息。在部分标题的左侧,我想要输入的日期(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 日星期三_________今天|

【问题讨论】:

    标签: ios objective-c uitableview sections


    【解决方案1】:

    titleForHeaderInSection 无法做到这一点。您需要改为实现viewForHeaderInSection。返回一个带有两个标签的视图——一个在左边,一个在右边。当然,如果您想要的话,您还需要复制标准表节标题的默认外观。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      相关资源
      最近更新 更多