0

私は新しい iOS プログラマーです。今、.plistファイルを使用してテーブルを作成しようとしています..しかし、セルをテーブルビューに挿入して.plistファイルに更新する方法がわからないという問題に直面しました。[+] cell をクリックしたときに、テーブルにセルを追加する必要があります。これが私の.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

テーブルビューのいくつかのプロパティを変更する必要があるだけです

  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;
    } } returnYES;
    }

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/に新しいレコードを追加するチュートリアルは次のとおりです。

于 2013-09-20T04:37:23.810 に答える