我的custom Picker
有优先级:低,中和高。
我custom Table
上的main_View
有两个标签:1项目名称和其他项目优先级。
当用户选择优先级并插入所需数据时,他会导航到main_View
我获取项目名称和项目优先级。
我确实做到了这一点,但我想要的是main_View
上的项目应根据他们的优先级排列,即最高,中等然后的低
static NSString CellIdentifier = @"Cell";
RecordList *records = [tablecontentArray objectAtIndex:indexPath.row];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *xibPath = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id fileObjects in xibPath)
{
cell = (CustomCell)fileObjects;
}
}
cell.itemName.text =records.toDoItem;
cell.itemPriority.text = records.toDoPriority;
return cell;
答案 0 :(得分:0)
如果您使用coreData,则可以将UITableView
扩展为NSFetchedResultsController
首先在包含UITableView
// MyClass.h
MyClass : UITableViewController<NSFetchedResultsControllerDelegate>
{
NSFetchedResultsController *fetchedResultsController;
// ...
}
// MyClass.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSError *error = nil;
fetchedResultsController = nil;
if( ![[self fetchedResultsController] performFetch:&error] )
{
// Log the error and/or do something with it
abort();
}
[self.tableView reloadData];
}
- (NSFetchedResultsController *)fetchedResultsController
{
if( !fetchedResultsController )
{
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RecordList"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"toDoPriority" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"toDoItem" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[sortDescriptor1 release];
[sortDescriptor2 release];
[fetchRequest setSortDescriptors:sortDescriptors];
// add predicate if you need
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"do I need a predicate?", things];
[fetchRequest setPredicate:predicate];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:@"MyRecordsCache"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptors release];
}
return fetchedResultsController;
}
-
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
static NSString CellIdentifier = @"Cell";
RecordList *records = [fetchedResultsController objectAtIndexPath:indexPath];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *xibPath = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = (CustomCell *)[xibPath objectAtIndex:0];
}
cell.itemName.text =records.toDoItem;
cell.itemPriority.text = records.toDoPriority;
return cell;
}
答案 1 :(得分:0)
-(void)viewWillAppear:(BOOL)animated
{
NSManagedObjectContext *objMOC = [appDelegate managedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"RecordList" inManagedObjectContext:objMOC]];
tablecontentArray = [[NSMutableArray alloc] initWithArray:[objMOC executeFetchRequest:fetch error:nil]];
sortArray = [[NSMutableArray alloc] init];
for (RecordList *strH in tablecontentArray) {
if ([strH.toDoPriority hasPrefix:@"H"]) {
[sortArray addObject:strH];
}
}
for (RecordList *strH in tablecontentArray) {
if ([strH.toDoPriority hasPrefix:@"M"]) {
[sortArray addObject:strH];
}
}for (RecordList *strH in tablecontentArray) {
if ([strH.toDoPriority hasPrefix:@"L"]) {
[sortArray addObject:strH];
}
}
[homeTable reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return sortArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
RecordList *records = [sortArray objectAtIndex:indexPath.row];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *xibPath = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id fileObjects in xibPath) {
cell = (CustomCell*)fileObjects;
}
}
cell.itemName.text =records.toDoItem;
cell.itemPriority.text = records.toDoPriority;
return cell;
}