2013-10-23 66 views
0

可能是一个渡轮容易(愚蠢)的问题,但我现在坚持四个几个小时。我搜索了很多项目,但我不知道我做错了什么。 我最近开始为IOS开发。不能从视图中填充表

我在做什么:我有一个实用程序的应用程序与主视图中的一个表。我试图用代码动态地填充表格。

.h文件中

#import "FlipsideViewController.h" 
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UITableViewDataSource,UITableViewDelegate> 
{ 
    NSArray *JSONArray; 
} 

@property (nonatomic, weak) IBOutlet UITableView *tableview; 
@property (nonatomic, retain) IBOutlet NSArray *JSONArray; 
@property (nonatomic, retain) IBOutlet NSArray *dynamicTable; 

@end 

.m文件

@interface MainViewController() { 
    NSMutableArray *_objects; 
} 
@end 

@implementation MainViewController 

@synthesize JSONArray; 
@synthesize tableview; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.tableview.delegate = self; 

    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 
    [_objects insertObject:[NSDate date] atIndex:0]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tableview reloadData]; 
    }); 
} 

为了填补我使用的默认IOS例子(娄后)

当我启动应用程序表中, numberOfRowsInSection,cellForRowAtIndexPath等...不加载。我做错了什么?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _objects.count; 
} 

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

    NSDate *object = _objects[indexPath.row]; 
    cell.textLabel.text = [object description]; 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_objects removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 

    } 
} 

回答

2

您忘记将该类设置为数据源。尝试使用此代码:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.tableview.delegate = self; 
    self.tableview.datasource = self; // <-- You forgot this 

    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 

    [_objects insertObject:[NSDate date] atIndex:0]; 

    [self.tableview reloadData]; 
} 
2

你忘了在viewDidLoad中的数据源委托:

[self.tableview setDataSource:self]; 

实际上有用来处理数据,另一个对付一个的TableView一个两位代表与表格视图进行交互。