【问题标题】:Creating an editable Table View in iOS?在 iOS 中创建可编辑的表格视图?
【发布时间】:2013-05-13 18:14:56
【问题描述】:

我正在尝试使用 .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 的身份检查器中,我将类从 UIView 更改为 UITableView

我将视图与数据源和/或委托连接(我尝试了所有组合)。

但是,我在电话上总是一无所获。我没有得到表格视图,只是一个空的灰色屏幕。

任何想法我做错了什么?上面的代码在一个使用故事板的项目中工作,现在我正试图让它与 .xib 文件一起工作。

截图:

【问题讨论】:

  • notes 数组是否包含任何数据? tableView 至少需要返回一行。向数组中添加一个对象并进行测试。
  • 在应用程序的第一次启动中,表格视图是空的。这并不意味着认为它不应该被加载。如您所见,应该加载一个可编辑的表格视图、一个栏、一个编辑按钮、一个添加按钮等。我不明白您的第二点,这似乎更重要。你能解释一下你的意思吗?
  • 它可能已加载,但没有返回单元格
  • 忽略第二部分,因为您使用的是 tableViewController。对于那个很抱歉。 tableView 是否分配给 xib 中的任何内容?委托需要指向您的班级。
  • 我将 xib 分配给委托人

标签: ios uitableview uiview uiviewcontroller


【解决方案1】:

我建议查看免费的 Sensible TableView 框架。将自动处理显示您的数组并代表您处理所有插入/删除。为我节省了大量时间。

【讨论】:

    【解决方案2】:

    经过一些工作,我能够做到并决定将其公开:

    Code on 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
    

    【讨论】:

    • 请在此处发布代码,而不是提供代码链接。谢谢 :)
    • 我在答案中添加了代码(除了 GitHub 存储库)@SaghirA.Khatri
    【解决方案3】:

    将至少一项添加到您的数组中,否则将不显示任何内容(当前您的 cellForRowAtIndexPath 正在返回 0)。另外,请确保 tableView 的委托已分配给您在 IB 中的类。

    【讨论】:

    • 你错了。使用故事板,我可以看到一个空的表格视图。现在我什么也没看到。就像没有加载一样。我无法与应用程序交互。应该有一个添加按钮,用户可以在其中添加 smth 到表视图等,但现在什么都没有加载。一开始表格视图应该是空的
    • 添加了一个截图,这样你就可以看到连接
    • 在告诉帮助你的人“你错了”之前,请先听听我们在说什么。首先,在您的屏幕截图中,您没有将数据源和委托设置为文件所有者。先做这个。其次,添加一个视图然后将tableView添加到视图中......
    猜你喜欢
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多