【发布时间】:2014-01-12 05:09:38
【问题描述】:
在我的 tableView 中,我有固定的部分和固定的部分标题。这是我这部分要求的代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;
}
+ (NSString*) titleForHeaderForSection:(int) section
{
switch (section)
{
case 0 : return @"Overdue";
case 1 : return @"Today";
case 2 : return @"Tomorrow";
case 3 : return @"Upcoming";
case 4 : return @"Someday";
case 5 : return @"Completed";
//default : return [NSString stringWithFormat:@"Section no. %i",section + 1];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [CollapsableTableViewViewController titleForHeaderForSection:section];
}
这部分工作正常,但现在我想用核心数据对象填充这些部分。 对于这部分,在获取结果之前,我有以下代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
switch (section)
{
case 2 : return 3;
case 3 : return 30;
default : return 3;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
switch (indexPath.row)
{
case 0 : cell.textLabel.text = @"First Cell"; break;
case 1 : cell.textLabel.text = @"Second Cell"; break;
case 2 : cell.textLabel.text = @"Third Cell"; break;
case 3 : cell.textLabel.text = @"Fourth Cell"; break;
case 4 : cell.textLabel.text = @"Fifth Cell"; break;
case 5 : cell.textLabel.text = @"Sixth Cell"; break;
case 6 : cell.textLabel.text = @"Seventh Cell"; break;
case 7 : cell.textLabel.text = @"Eighth Cell"; break;
default : cell.textLabel.text = [NSString stringWithFormat:@"Cell %i",indexPath.row + 1];
}
//cell.detailTextLabel.text = ...;
return cell;
}
填充这些部分也可以正常工作......但我真正的要求是使用来自核心数据实体的对象填充这些部分。这意味着,我必须替换这些行:
switch (section)
{
case 2 : return 3;
case 3 : return 30;
default : return 3;
}
与其他代码行一起以获得节中的实际行数。 以及以下代码行:
switch (indexPath.row)
{
case 0 : cell.textLabel.text = @"First Cell"; break;
case 1 : cell.textLabel.text = @"Second Cell"; break;
case 2 : cell.textLabel.text = @"Third Cell"; break;
case 3 : cell.textLabel.text = @"Fourth Cell"; break;
case 4 : cell.textLabel.text = @"Fifth Cell"; break;
case 5 : cell.textLabel.text = @"Sixth Cell"; break;
case 6 : cell.textLabel.text = @"Seventh Cell"; break;
case 7 : cell.textLabel.text = @"Eighth Cell"; break;
default : cell.textLabel.text = [NSString stringWithFormat:@"Cell %i",indexPath.row + 1];
}
替换为其他代码行来调用真实对象的属性。
我已经在 y 项目中包含了所有核心数据堆栈以及 NSFetchedResultsController。 核心数据实体有两个属性:tdText、tdDate。 固定部分与 tdDate 属性相关。
【问题讨论】:
-
你似乎一次又一次地围绕着同样的问题 :-) - 但是如果你已经有一个获取的结果控制器,你为什么不在使用它数据源方法(numberOfSections、numberOfRowsInSection、cellForRowAtIndexPath、...)。你已经在stackoverflow.com/questions/20249098/… 中做到了,我(希望)展示了如何将项目分组为“今天”、“明天”等。
-
(续)我不想听起来粗鲁(我——当然还有其他人——如果可以的话,会有所帮助),但也许你可以尝试使用 对您之前的问题给出的答案(例如stackoverflow.com/questions/21048835/…),并更清楚地描述您的具体问题所在。
-
谢谢@MartinR,你听起来并不粗鲁,我知道你愿意帮助我,但问题在于我。我只能遵循一些教程并对教程代码进行微小的更改。在你和其他人给我的一些答案中,我只理解了一半,在我的 iOS 级别,我需要更清晰的答案,我需要将我的问题分解成更小的部分以完全理解答案。我的问题的另一部分是英语不是我的母语,也许我没有用正确的词来解释我真正需要的东西。
-
@MartinR,我决定按照 Caleb 的建议创建六个单独的提取,每个类别一个(过期、今天、明天、某天、即将到来、已完成)。他们每个人都有一个给定的谓词来选择那些适合每个类别的对象。我已将它们编号为 frc1,frc2,...现在的问题是如何使用它们来分配每个部分的结果来代替我的 cellForRowAtIndexPath 方法中的当前开关循环?
-
这是获取核心数据/FRC 等基础知识的好指南youtube.com/watch?v=G36_91H4CKE