ios - 无法从 View 中填充表格

标签 ios iphone objective-c uitableview

可能是一个简单(愚蠢)的渡轮问题,但我现在被困了四个小时。我在 SO 上搜索了很多项目,但我不知道我做错了什么。 我最近开始为 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) {

    }
}

最佳答案

您忘记将数据源委托(delegate)放入 viewDidLoad 中:

[self.tableview setDataSource:self];

TableView 实际上有两个委托(delegate),一个用于处理数据,另一个用于处理与 TableView 的交互。

关于ios - 无法从 View 中填充表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19538532/

相关文章:

ios - Xcode 在打开 Storyboard 时崩溃

ios - UITabViewController 工作,但现在我的其他 segueys 停止工作

iphone - iOS 6 - YouTube 应用

objective-c - iOS CoreGraphics : Draw arc, 根据相交弦定理确定弧角

iphone - 如何在 iOS 中获取时区列表,就像我们在注册 Web 应用程序时获取的一样

iPhone:viewWillAppear 仅在从其他 View 返回时出现

IOS,swift - UICollectionReusableView 不工作

IOS,在 UIScrollView 内的 UILabel 上设置手势点击

Objective-C,释放一个对象两次?

ios - 加载时检索 NSURL 数据