我的Master View controller
问题没有出现在我的故事板中。
通过搜索这个(真棒)网站,我学会了检查我的numberOfSectionsInTableView
和tableView:numberOfRowsInSection:
方法。我相信我的问题是tableView:numberOfRowsInSection:
正在返回'int 0'
。
非常感谢任何帮助。
我的MasterViewController.m:
#import "ParentMasterViewController.h"
#import "ParentDetailViewController.h"
#import "NewEventDataController.h"
#import "NewEvent.h"
@interface ParentMasterViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
@implementation ParentMasterViewController
- (void)awakeFromNib
{
[super awakeFromNib];
self.dataController = [[NewEventDataController alloc] init];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataController countOfList];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSDateFormatter *formatter = nil;
if (formatter == nil)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // forIndexPath:indexPath];
// note: Add some code like this to create a new cell if there are none to reuse
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NewEvent *eventAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:eventAtIndex.event];
[[cell detailTextLabel] setText:[formatter stringFromDate:(NSDate *)eventAtIndex.date]];
return cell;
}
另外,我的NewEventDataController.m:
#import "NewEventDataController.h"
#import "NewEvent.h"
@interface NewEventDataController ()
- (void)initializeDefaultDataList;
@end
@implementation NewEventDataController
- (void)initializeDefaultDataList
{
NSMutableArray *eventList = [[NSMutableArray alloc] init];
self.masterNewEventList = eventList;
NewEvent *event;
NSDate *today = [NSDate date];
event = [[NewEvent alloc] initWithName:@"Event" date:today];
[self addNewEventWithEvent:event];
}
- (void)setMasterNewEventList:(NSMutableArray *)newList
{
if (_masterNewEventList != newList)
_masterNewEventList = [newList mutableCopy];
}
- (id)init
{
if (self = [super init])
{
[self initializeDefaultDataList];
return self;
}
return nil;
}
- (NSUInteger)countOfList
{
return [self.masterNewEventList count];
}
- (NewEvent *)objectInListAtIndex:(NSUInteger)theIndex
{
return [self.masterNewEventList objectAtIndex:theIndex];
}
- (void)addNewEventWithEvent:(NewEvent *)event
{
[self.masterNewEventList addObject:event];
}
@end
先谢谢你一起来看看。
答案 0 :(得分:1)
参考:Table View Programming Guide
如果我没有解释错误而不是从某处复制此代码而忘记更改此方法的实现。将其更改为每行包含一个部分或注释掉整个方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}