如何更改数组并使其显示UITableView中的更改?

时间:2014-03-09 23:27:39

标签: ios objective-c uitableview nsmutablearray

要查看我的tableview中的项目,我从数组中加载它们,但是当我安排表格时,更改不会保留。它首先工作,但是当我点击其中一个单元格时,它会加载原始订单中的数据。

这是我的tableview控制器。我从这个Apple开发人员教程中获得了几乎所有代码:

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html#//apple_ref/doc/uid/TP40011343-CH10-SW1

#import "NoteViewController.h"
#import "Note.h"
#import "NewNoteViewController.h"

@interface NoteViewController ()

@property NSMutableArray *notes;

@end

@implementation NoteViewController

- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
    NewNoteViewController *source = [segue sourceViewController];
    Note *notee = source.note;
    if (notee != nil)
    {
        [self.notes addObject:notee];
        [self.tableView reloadData];
    }
}

/*
- (void)loadInitialData
{
    Note *item1 = [[Note alloc] init];
    item1.itemName = @"Note 1";
    [self.notes addObject:item1];
}
*/

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.notes = [[NSMutableArray alloc] init];
    //[self loadInitialData];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.notes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NotePrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    Note *note = [self.notes objectAtIndex:indexPath.row];
    cell.textLabel.text = note.itemName;

    // Configure the cell...

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source
        [self.notes removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];
    }
}

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{

}

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //Note *tappedItem = [self.notes objectAtIndex:indexPath.row];
    [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}

@end

1 个答案:

答案 0 :(得分:1)

取消注释- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath您已启用移动行。您需要实现该方法来交换notes数组中的项目。像:

id *note = [self.notes objectAtIndex:sourceIndexPath.row];
[self.notes removeObjectAtIndex:fromIndexPath.row];
[self.notes insertObject:note atIndex:toIndexPath.row];