如何使用.plist文件将单元格插入到我的列表中

时间:2013-09-20 03:54:50

标签: ios

我是一名新的ios程序员。现在我正在尝试使用.plist文件创建一个表..但我遇到了一个问题 我不知道如何将单元格插入表视图并将其更新为.plist文件。 我希望我的表必须在单击[+]单元格时添加一个单元格。 这是我的.m文件。请看看这个

#import "tableRecordings.h"

@interface tableRecordings ()

@end

@implementation tableRecordings

@synthesize arrayOriginal;
@synthesize arForTable;
UITableView* tableView;
NSArray *ar;
NSUInteger row;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    ar= [[NSArray alloc] init];
    tableView=[[UITableView alloc] init] ;
    NSDictionary *dTmp=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
    self.arrayOriginal=[dTmp valueForKey:@"Objects"];

    self.arForTable=[[NSMutableArray alloc] init];

    [self.arForTable addObjectsFromArray:self.arrayOriginal];}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arForTable count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

    cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];

    [cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];

    return cell;
}

#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.tableView=tableView;
    NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
    if([d valueForKey:@"Objects"]) {
        ar=[d valueForKey:@"Objects"];
           row=indexPath.row;
        BOOL isAlreadyInserted=NO;

        for(NSDictionary *dInner in ar ){
            NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
            isAlreadyInserted=(index>0 && index!=NSIntegerMax);
            if(isAlreadyInserted) break;
        }

        if(isAlreadyInserted) {
            [self miniMizeThisRows:ar];
            [tableView cellForRowAtIndexPath:indexPath].textColor=[UIColor blackColor];

        } else {
            [tableView cellForRowAtIndexPath:indexPath].textColor=[UIColor blueColor];
            NSUInteger count=indexPath.row+1;
            NSMutableArray *arCells=[NSMutableArray array];
            for(NSDictionary *dInner in ar ) {
                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                [self.arForTable insertObject:dInner atIndex:count++];
            }
            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    else if ([d valueForKey:@"name"]) {
        if ([[d valueForKey:@"name"] isEqualToString:@"[+]"]){


            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Insert An Activity" message:@"Put Activity Here" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            [alert show];
            [alert release];
        }

    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
   // [tableView insertRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>];
}
-(void)miniMizeThisRows:(NSArray*)ar{

    for(NSDictionary *dInner in ar ) {
        NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
        NSArray *arInner=[dInner valueForKey:@"Objects"];
        if(arInner && [arInner count]>0){
            [self miniMizeThisRows:arInner];
        }

        if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
            [self.arForTable removeObjectIdenticalTo:dInner];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:
                                                    [NSIndexPath indexPathForRow:indexToRemove inSection:0]
                                                    ]
                                  withRowAnimation:UITableViewRowAnimationRight];
        }
    }
}


- (void)viewDidUnload
{
    [super viewDidUnload];
}


- (void)dealloc {

    [super dealloc];
}
@end

1 个答案:

答案 0 :(得分:0)

嗯,你只需要改变tableview的一些属性

  1. 关于编辑点击事件,将您的桌面视图设置为编辑模式

    [super setEditing:YES animated:YES]; [self.mainTableView setEditing:YES animated:YES];

  2. 启用受尊重的单元格或部分的编辑模式

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    //如果您不希望指定的项目可编辑,请返回NO    if(indexPath.section == 0){       if(indexPath.row == 0){         返回NO;
          }    }    returnYES;
    }

  3. 3.配置编辑模式 - UI控件:

    if ([self.tableView isEditing]) {
      // If the tableView is already in edit mode, turn it off. Also change the title of the       button to reflect the intended verb (‘Edit’, in this case).
       [self.tableViewsetEditing:NOanimated:YES];
       [self.editButtonsetTitle:@"Edit"forState:UIControlStateNormal];
    } else {
       [self.editButtonsetTitle:@"Done"forState:UIControlStateNormal];
    
       // Turn on edit mode
    
       [self.tableViewsetEditing:YESanimated:YES];
    }
    

    4.配置编辑模式 - TableView:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Get the managedObjectContext from the AppDelegate (for use in CoreData Applications)
    AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = appdelegate.managedObjectContext;
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    
       YourObject *object = [self.dataSourceArray objectAtIndex:indexPath.row];
       [self.dataSourceArray removeObjectAtIndex:indexPath.row];
       // You might want to delete the object from your Data Store if you’re using CoreData
       [context deleteObject:pairToDelete];
       NSError *error;
       [context save:&error];
       // Animate the deletion
       [tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
       // Additional code to configure the Edit Button, if any
       if (self.dataSourceArray.count == 0) {
           self.editButton.enabled = NO;
           self.editButton.titleLabel.text = @”Edit”;
       }
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
       // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       YourObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Header" inManagedObjectContext:context];
       newObject.value = @”value”;
       [context save:&error];
       [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
       if (self.dataSourceArray.count > 0) {
          self.editButton.enabled = YES;
       }
    }
    

    }

    这会在表格视图中添加新行

    要更新.plist文件,您需要编写代码以更新此方法中的plist文件。

     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    

    根据UITableViewCellEditingStyle,即插入或删除,您可以更新您的plist。

    以下是向plist添加新记录的教程 http://www.theappcodeblog.com/2011/05/30/property-list-tutorial-using-plist-to-store-user-data/