2013-05-13 26 views
0

我正尝试在iOS中使用.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文件一起工作。

截图:

enter image description here

+1

notes数组是否包含任何数据? tableView需要返回至少一行。将一个对象添加到数组并进行测试。 – 2013-05-13 18:19:40

+0

那么在应用程序首次启动时,表格视图是空的。那并不意味着它不应该被加载。正如你所看到的,应该加载一个可编辑的表格视图,一个酒吧,一个编辑按钮,一个添加按钮等。我不明白你的第二点,这似乎更重要。你能解释一下你的意思吗? – LolaEnaMilo 2013-05-13 18:22:36

+1

它可能已加载,但不会返回单元格 – 2013-05-13 18:23:06

回答

1

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

0

添加至少一个项目到您的阵列或什么都不会被显示出来(目前你的cellForRowAtIndexPath将返回0)。此外,请确保tableView的委托在IB中分配给您的班级。

+0

你错了。随着故事板我可以看到一个空的表格视图。现在我什么也没看见。像没有加载。我无法与应用程序进行交互。应该有一个添加按钮,用户可以在表格视图中添加smth等,但现在没有加载。在开始时,表格视图应为空 – LolaEnaMilo 2013-05-13 18:32:13

+0

添加了截图,以便您可以看到连接 – LolaEnaMilo 2013-05-13 18:36:21

+0

在告诉人们正在帮助您“您错了”之前,请听我们在说什么。首先,在您的屏幕截图中,您没有将dataSource和委托设置为文件所有者。先做这件事。其次,添加一个视图,然后将tableView添加到视图中... – 2013-05-13 19:33:39

0

一些工作后,我能够做到这一点,并决定把它公开:

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 
+1

plz post代码在这里,而不是提供代码的链接。 Thx :) – 2013-12-31 04:56:28

+1

我在答案中添加了代码(除了GitHub存储库)@ SaghirA.Khatri – Diego 2014-01-04 03:18:33