- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我之前发过类似的问题,请看这个链接UITableView sections are not ordered as expected我需要解决的问题如下:我有一个带有自定义部分的 tableView。部分标题取自在名为 ToDoItem 的 NSManagegObject 子类中定义的临时属性。 transient 属性称为 sectionIdentifier。在我的 tableView viewController 中,我有一个带有两个 NSSortDescriptor 的 fetchedResultsController 来排序对象:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"todoDueDate" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]initWithKey:@"todoName" ascending:YES];
考虑到 transient 属性不能用作 NSSortDescriptor 的 initWithKey 值,现在根据 todoDueDate 的属性值而不是 sectionIdentifier 值对部分进行排序,这应该是我预期的顺序。我将两个分类器的代码放在代码下方,第一个是 ToDoItem 类的 sectionIdentifier 定义,第二个是 tableView vieController 类。
-(NSString *)sectionIdentifier{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if ([self.isSomeDay isEqualToString:@"noissomeday"]){//SI NO ES SOMEDAY
if (!tmp){
//NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDate *today = [NSDate date];
NSDate *date = self.todoDueDate;
NSDateComponents *date1Components = [calendar components:comps
fromDate: today];
NSDateComponents *date2Components = [calendar components:comps
fromDate: date];
today = [calendar dateFromComponents:date1Components];
date = [calendar dateFromComponents:date2Components];
NSInteger daysAfterToday = [calendar components:NSDayCalendarUnit
fromDate:today toDate:date options:0].day;
// NSString *section;
if (daysAfterToday < 0) {
tmp = @"0";
} else if (daysAfterToday == 0) {
tmp = @"1";
} else if (daysAfterToday > 0 && daysAfterToday < 2) {
tmp = @"2";
} else {
tmp = @"3";
}
NSLog(@"TODAY = %@", today);
NSLog(@"DATE = %@", date);
NSLog(@"DAYS AFTER TODAY = %ld",(long)daysAfterToday);
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
}
//no is someday
else if ([self.isSomeDay isEqualToString:@"issomeday"]){
tmp = @"4";
}
NSLog(@"Tmp= %@",tmp);
return tmp;
}
也就是我要获取的section order:
1. OVERDUE, sectionIdentifier = 0
2. TODAY, sectionIdentifier = 1
3. TOMORROW, sectionIdentifier = 2
4. UPCOMING, sectionIdentifier = 3
5. SOMEDAY, sectionIdentifier = 4
ToDoItemsTableViewController.m
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 1;
}
else
{
return [[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else {
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
ToDoItem *toDoItem = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
NSLog(@"Configuring cell to show search results");
toDoItem = [self.searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.todoName;
NSDate *fechaToDO = toDoItem.todoDueDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEEE, dd MMMM YYYY"];
NSString *fechaToDo = [dateFormatter stringFromDate:fechaToDO];
NSString *valorSomeDay = toDoItem.isSomeDay;
if ([valorSomeDay isEqualToString:@"issomeday"]){
cell.detailTextLabel.text = @"Someday";
}
else {
cell.detailTextLabel.text = fechaToDo;
}
}
else
{
ToDoItem *todoitem = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = todoitem.todoName;
NSDate *fechaToDO = todoitem.todoDueDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEEE, dd MMMM YYYY"];
NSString *fechaToDo = [dateFormatter stringFromDate:fechaToDO];
NSString *valorSomeDay = todoitem.isSomeDay;
if ([valorSomeDay isEqualToString:@"issomeday"]){
cell.detailTextLabel.text = @"Someday";
}
else {
cell.detailTextLabel.text = fechaToDo;
}
}
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *header = @"customHeader";
UITableViewHeaderFooterView *vHeader;
vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
if (!vHeader) {
vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor = [UIColor redColor];
vHeader.textLabel.textColor = [UIColor whiteColor];
vHeader.contentView.backgroundColor = [UIColor redColor];
}
if (section == 0) {
vHeader.textLabel.backgroundColor = [UIColor redColor];
vHeader.textLabel.textColor = [UIColor whiteColor];
vHeader.contentView.backgroundColor = [UIColor redColor];
}
else if (section == 1) {
vHeader.textLabel.backgroundColor = [UIColor orangeColor];
vHeader.textLabel.textColor = [UIColor blueColor];
vHeader.contentView.backgroundColor = [UIColor orangeColor];
}
else if (section == 2) {
vHeader.textLabel.backgroundColor = [UIColor greenColor];
vHeader.textLabel.textColor = [UIColor whiteColor];
vHeader.contentView.backgroundColor = [UIColor greenColor];
}
else if (section == 3) {
vHeader.textLabel.backgroundColor = [UIColor greenColor];
vHeader.textLabel.textColor = [UIColor whiteColor];
vHeader.contentView.backgroundColor = [UIColor greenColor];
}
else if (section == 4) {
vHeader.textLabel.backgroundColor = [UIColor blueColor];
vHeader.textLabel.textColor = [UIColor whiteColor];
vHeader.contentView.backgroundColor = [UIColor blueColor];
}
vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
return vHeader;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (tableView == self.searchDisplayController.searchResultsTableView){
NSString *valor = [NSString stringWithFormat:@"S E A R C H R E S U L T S (%d)",[self.searchResults count]];
return valor;
}
else {
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section];
NSString *sectionname = [theSection name];
if ([sectionname isEqualToString:@"0"]){
NSString *valor = [NSString stringWithFormat:@"O V E R D U E (%d)", [self.tableView
numberOfRowsInSection:section]];
return valor;
}
else if ([sectionname isEqualToString:@"1"]){
NSString *valor = [NSString stringWithFormat:@"T O D A Y (%d)", [self.tableView
numberOfRowsInSection:section]];
return valor;
}
else if ([sectionname isEqualToString:@"2"]){
NSString *valor = [NSString stringWithFormat:@"T O M O R R O W (%d)", [self.tableView
numberOfRowsInSection:section]];
return valor;
}
else if ([sectionname isEqualToString:@"3"]){
NSString *valor = [NSString stringWithFormat:@"U P C O M I N G (%d)", [self.tableView
numberOfRowsInSection:section]];
return valor;
}
else if ([sectionname isEqualToString:@"4"]){
NSString *valor = [NSString stringWithFormat:@"S O M E D A Y (%d)", [self.tableView
numberOfRowsInSection:section]];
return valor;
}
if ([[self.fetchedResultsController sections]count]>0){
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo name];
}
else{
return nil;
}
}
}
#pragma mark - Fetched Results Controller Section
-(NSFetchedResultsController*)fetchedResultsController{
if (_fetchedResultsController != nil){
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSManagedObjectContext *context = self.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ToDoItem" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"todoDueDate" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]initWithKey:@"todoName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor,sortDescriptor1, nil];
fetchRequest.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionIdentifier" cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
最佳答案
我假设“SOMEDAY”条目是那些 todoDueDate
是“未指定”的条目。问题是第一个排序描述符“todoDueDate”必须兼容节标识符。所以你不能使用两个单独的属性 todoDueDate
和isSomeDay
部分。
因此,不要使用单独的属性 isSomeDay
,您应该分配这些对象一个遥远 future 的值:
self.todoDueDate = [NSDate distantFuture];
以便它们自动排在所有其他对象之后。然后你可以在 sectionIdentifier
方法中做这样的事情:
if ([self.todoDueDate isEqualToDate:[NSDate distantFuture]) {
tmp = @"4";
} else {
// ... your other checks for overdue, today, tomorrow, upcoming
}
同样在 viewForHeaderInSection
中,您不应该检查节号,但对于部分名称(“0”、“1”、...)。原因是一个部分可能是空的:如果没有“OVERDUE”对象,则“TODAY”是第 0 部分。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
NSString *tmp = [theSection name];
if ([tmp isEqualToString:@"0"]) {
// OVERDUE
} else if ([tmp isEqualToString:@"1"]) {
// TODAY
} else
// and so on ...
}
关于ios - 使用 transient 属性对 UITableView 部分进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222361/
我知道这个问题已经被问过很多次了,但我找不到适合我的答案。 我在 Spring Roo 应用程序中有两个实体,它们处于多对多关系、发布和组件中。 首先,我通过以下方式获取现有版本的实例 selecte
我正在尝试将用户详细信息存储到下表中:user、role、user_role。尝试保存详细信息时,它会引发以下错误。 Error during managed flush [org.hibernate
我有两个 hibernate 实体 Coupon 和 CouponHistory,在 CouponHistory 和 Coupon 之间具有单向关系。 @Entity @Table(name = "v
我在外键 dimension_id 之一的表中有表 product。所以在服务层编写我的测试用例时它显示了错误。 这是我的测试用例 @Transactional(propagation = Propa
在 ARM 架构手册中提到缓存可以是 transient 的和非 transient 的,并且它是由实现定义的。我无法理解关于缓存的 transient 内存的概念和使用。我正在尝试编写启用 MMU
我有 2 个域模型和一个 Spring REST Controller ,如下所示: @Entity public class Customer{ @Id private Long id; @OneT
我知道这个问题在Stackoverflow中有很多问题,但是即使有很多答案,这些答案也帮不了我什么,也没有找到答案。 在我的WebAPP中,它可以正常工作,但是当我将其转换为API时,它失败了(主题标
我有以下域名 class User { Boolean accountLocked String password Boolean passwordExpired Bo
我写了一个 elisp 宏,在 transient-mark-mode 中保留区域: (defmacro keep-region (command) "Wrap command in code t
这是我的员工类(class): @Entity public class Employee { @Id @GeneratedValue private int id; private String f
我正在通读 Windows Phone 7.5 Unleashed,有很多代码看起来像这样(在页面的代码隐藏中): bool loaded; protected override void OnNav
我有一个自连接员工实体类,其中包含与其自身相关的 id、name 和 ref 列。我想创建它的新实例并将其保存到数据库。 首先我创建了一个 Employee 类的实例并将其命名为 manager。然后
我有一个用于添加新公寓的表单,在该表单中我有一个下拉列表,用户可以在其中选择负责的人员。 显然,当您从下拉列表中选择并尝试保存公寓时,我的应用程序认为该人已被修改。它给了我下面的错误,指示我应该首先保
我正在尝试保存一个复杂的对象,该对象内部有许多引用元素,而且它在大多数情况下都能完美运行。 但是在某些情况下,我们会遇到以下异常, object references an unsaved trans
我遇到了一些可能的问题答案,但这是关于从 Hibernate 3.4.0GA 升级到 Hibernate 4.1.8 的问题。所以这曾经在以前的版本下工作,我已经四处搜索了为什么它在这个新版本中出现了
似乎一遍又一遍地问这个问题,我仍然找不到解决我问题的答案。我在下面有一个域模型。每个新创建或更新的“安全用户”都需要我确保其具有配置文件,如果没有,则创建一个新的配置文件并分配给它。 配置文件的要求相
我很难调试为什么 JPA 不级联我的 @ManyToMany 关系。我发现的所有答案都与缺少级联语句有关。但我确实拥有它们并且仍然得到: Caused by: org.hibernate.Transi
例如,当我使用 transient 通过更改 translate(x, y) 的值来实现 2s 持续时间的动画时。如何获取0.5s时刻translate(x, y)的当前值? 最佳答案 我认为你做不到
我在尝试保存属于多对多关联的对象时收到 TransientObjectException。我有点理解为什么会这样,但想了解如何正确完成我正在尝试做的事情。 简而言之,我正在尝试做的事情: 我的应用程序
transient final int 和 transient final Integer 有什么不同。 使用 int: transient final int a = 10; 序列化前: a = 1
我是一名优秀的程序员,十分优秀!