0

我是一个新的 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
4

1 回答 1

0

那么你只需要改变tableview的一些属性

  1. 在编辑点击事件中将您的表格视图设置为编辑模式

    [super setEditing:YES 动画:YES]; [self.mainTableView setEditing:YES 动画:YES];

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

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

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.配置编辑模式——表格视图:

- (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/

于 2013-09-20T04:37:23.810 回答