0

.xib ファイルを使用して iOS で編集可能なテーブル ビューを作成しようとしています。

私のコードは次のようになります:

viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *notes;

@end

viewController.m

#import "ViewController.h"
#import "Note.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setEditing:NO animated:NO];
}

- (void)viewWillAppear:(BOOL)animated{

    self.notes = [[Note savedNotes]mutableCopy];
    [self.tableView reloadData];
}

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

#pragma mark - Button Events

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if (editing) {
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
        self.navigationItem.leftBarButtonItem = cancelButton;

        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(rightButtonPressed:)];

        self.navigationItem.rightBarButtonItem = doneButton;

    }else{
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                             target:self
                                                                                             action:@selector(addNoteButtonPressed:)];

        UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                                                                                   target:self
                                                                                   action:@selector(rightButtonPressed:)];

        self.navigationItem.rightBarButtonItem = editButton;
    }

}

- (void)rightButtonPressed:(id)sender{

    [self setEditing:!self.isEditing animated:YES];
}

- (void)cancelButtonPressed:(id)sender{
    [self setEditing:!self.isEditing animated:YES];
}

- (void)addNoteButtonPressed:(id)sender{
    //ATTViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ATTViewController"];
    //[self.navigationController pushViewController:viewController animated:YES];
}

#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 = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    Note *note = self.notes[indexPath.row];

    cell.textLabel.text = note.name;
    cell.detailTextLabel.text = note.event;

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

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


// 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
        Note *note = self.notes[indexPath.row];
        [note remove];
        [self.notes removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
    }
    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
    }
}


/*
 // 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 - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

.xib の ID インスペクターで、クラスをUIViewからUITableViewに変更します。

ビューをデータソースおよび/またはデリゲートに接続します(すべての組み合わせを試しました)。

しかし、私はいつも電話で何も得ません。テーブルビューが表示されず、空の灰色の画面が表示されます。

私が間違っていることは何ですか?上記のコードは、ストーリーボードを使用するプロジェクトで動作していましたが、現在、.xib ファイルで動作するようにしようとしています。

スクリーンショット:

ここに画像の説明を入力

4

3 に答える 3

1

いくつかの作業の後、私はそれを行うことができ、公開することにしました:

GitHub のコード

私はこれを行う方法を学んでいましたが、移動、削除、および追加をサポートしたかったのです。

最後の行に追加ボタンのみを示す例もあれば、移動/削除のみを示す例もありました。

私にとってトリッキーだったのは、同時に 3 つをサポートすることでした。

それが役に立てば幸い。

コードは次のとおりです。

EditTVC.h

#import <UIKit/UIKit.h>

@interface EditTVC : UITableViewController

@end

EditTVC.m

//
//  EditTVC.m
//  editTableViewTest
//

#import "EditTVC.h"

@interface EditTVC ()

@property (nonatomic) NSMutableArray *myItems;

@end

@implementation EditTVC

@synthesize myItems;

- (void)viewDidLoad
{
    [super viewDidLoad];

    myItems = [[NSMutableArray alloc] init];

    for (int i=0; i<5; i++)
    {
        [myItems addObject:[NSString stringWithFormat:@"Hey %d",i]];
    }

    NSLog(@"%@",myItems);


    // Display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    if (self.editing) {
        return myItems.count +1;
    }

    return myItems.count;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == myItems.count)
        return UITableViewCellEditingStyleInsert;
    else
        return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    if (indexPath.row == myItems.count)
        return NO;
    else
        return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    if (self.editing && indexPath.row == myItems.count)
    {
        cell.textLabel.text = @"Add ...";
    }
    else
    {
        cell.textLabel.text = myItems[indexPath.row];
    }

    return cell;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];

    if(editing)
    {
        //edit mode
    }

    else
    {
        //non-edit mode
    }

    [self.tableView reloadData];
}

// 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
        [myItems removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    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
        [myItems insertObject:[NSString stringWithFormat:@"Added %ld",(long)indexPath.row] atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    NSLog(@"%@",myItems);
}

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    if (toIndexPath.row == myItems.count) //we only check the toIndexPath because we made the AddCell not to respond to move events
    {
        id tmp = [myItems objectAtIndex:fromIndexPath.row];
        [myItems removeObjectAtIndex:fromIndexPath.row];
        [myItems insertObject:tmp atIndex:toIndexPath.row-1]; //to keep it in valid range for the NSMutableArray

        [self.tableView reloadData];
    }

    else
    {
        id tmp = [myItems objectAtIndex:fromIndexPath.row];
        [myItems removeObjectAtIndex:fromIndexPath.row];
        [myItems insertObject:tmp atIndex:toIndexPath.row];
    }

    NSLog(@"%@",myItems);
}



@end
于 2013-12-31T04:39:20.117 に答える
0

Add at least one item to your array or nothing will be displayed (currently your cellForRowAtIndexPath is returning 0). Also, make sure the tableView's delegate is assigned to your class in IB.

于 2013-05-13T18:21:35.083 に答える