1

我有一个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; 
4

2 回答 2

0

如果你使用 coreData,你可以扩展你UITableViewNSFetchedResultsController

首先在持有的类中添加委托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;
}
于 2013-05-28T07:45:39.187 回答
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;
}
于 2013-05-29T06:03:23.187 回答