我正在尝试使用 .xib 文件在 iOS中创建可编辑的表格视图。
我的代码如下所示:
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *notes;
@end
视图控制器.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 文件一起工作。
截屏 :