iOS tableView没有填充数据

Dev*_*Dev 2 uitableview ios

有人可以向我解释为什么这没有在viewController中填充tableView吗?我可以使其在UITableViewController中工作,但是当我制作带有tableView的UIViewController时,它不会填充数据。在我的情节提要中,我创建了一个具有tableview和table view单元格的视图控制器。当我运行程序时,我得到的只是我的空tableView。但是它在UITableViewController中很好用

。H

   //MainViewController.h
    #import <UIKit/UIKit.h>

    @interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
    {
        NSArray *topItems;
        NSMutableArray *subItems; // array of arrays

        int currentExpandedIndex;
    }
    @property (strong, nonatomic) IBOutlet UITableView *tableView;


    @end
Run Code Online (Sandbox Code Playgroud)

.m

    ///MainController.m
    #import "MainViewController.h"

    #define NUM_TOP_ITEMS 5
    #define NUM_SUBITEMS 6

    @interface MainViewController ()

    @end

    @implementation MainViewController

    - (id)init {
        self = [super init];

        if (self) {
            topItems = [[NSArray alloc] initWithArray:[self topLevelItems]];
            subItems = [NSMutableArray new];
            currentExpandedIndex = -1;

            for (int i = 0; i < [topItems count]; i++) {
                [subItems addObject:[self subItems]];
            }
        }
        return self;
    }

    #pragma mark - Data generators

    - (NSArray *)topLevelItems {
        NSMutableArray *items = [NSMutableArray array];

        for (int i = 0; i < NUM_TOP_ITEMS; i++) {
            [items addObject:[NSString stringWithFormat:@"Item %d", i + 1]];
        }

        return items;
    }

    - (NSArray *)subItems {
        NSMutableArray *items = [NSMutableArray array];
        int numItems = arc4random() % NUM_SUBITEMS + 2;

        for (int i = 0; i < numItems; i++) {
            [items addObject:[NSString stringWithFormat:@"SubItem %d", i + 1]];
        }

        return items;
    }

    #pragma mark - View management

    - (void)viewDidLoad {
        [super viewDidLoad];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    #pragma mark - Table view data source

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

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [topItems count] + ((currentExpandedIndex > -1) ? [[subItems objectAtIndex:currentExpandedIndex] count] : 0);
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *ParentCellIdentifier = @"ParentCell";
        static NSString *ChildCellIdentifier = @"ChildCell";

        BOOL isChild =
        currentExpandedIndex > -1
        && indexPath.row > currentExpandedIndex
        && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

        UITableViewCell *cell;

        if (isChild) {
            cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
        }
        else {
            cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
        }


        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier] ;
        }

        if (isChild) {
            cell.detailTextLabel.text = [[subItems objectAtIndex:currentExpandedIndex] objectAtIndex:indexPath.row - currentExpandedIndex - 1];
        }
        else {
            int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex)
            ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]
            : indexPath.row;

            cell.textLabel.text = [topItems objectAtIndex:topIndex];
            cell.detailTextLabel.text = @"";
        }

        return cell;
    }

    #pragma mark - Table view delegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        BOOL isChild =
        currentExpandedIndex > -1
        && indexPath.row > currentExpandedIndex
        && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

        if (isChild) {
            NSLog(@"A child was tapped, do what you will with it");
            return;
        }

        [self.tableView beginUpdates];

        if (currentExpandedIndex == indexPath.row) {
            [self collapseSubItemsAtIndex:currentExpandedIndex];
            currentExpandedIndex = -1;
        }
        else {

            BOOL shouldCollapse = currentExpandedIndex > -1;

            if (shouldCollapse) {
                [self collapseSubItemsAtIndex:currentExpandedIndex];
            }

            currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

            [self expandItemAtIndex:currentExpandedIndex];
        }

        [self.tableView endUpdates];

    }

    - (void)expandItemAtIndex:(int)index {
        NSMutableArray *indexPaths = [NSMutableArray new];
        NSArray *currentSubItems = [subItems objectAtIndex:index];
        int insertPos = index + 1;
        for (int i = 0; i < [currentSubItems count]; i++) {
            [indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
        }
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    }

    - (void)collapseSubItemsAtIndex:(int)index {
        NSMutableArray *indexPaths = [NSMutableArray new];
        for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
            [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

    }

    - (BOOL)prefersStatusBarHidden {
        return YES;
    }

    @end
Run Code Online (Sandbox Code Playgroud)

Ric*_*ick 5

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
Run Code Online (Sandbox Code Playgroud)

在UITableViewController中,由于它是主视图,因此会自动为您设置tableView的委托和数据源。但这不是UIViewController中的tableView(作为子视图)的情况。您需要手动添加2条语句。

编辑:

我将您的代码复制到一个新项目中,并发现了其他几个问题:

在此之前,请确保您的IB已连接到情节提要中的tableView,如meda所说。我以为你知道这一点。

  1. 在AppDelegate.m中,由于使用的是故事板(在我的情况下为main.storyboard),请使用以下代码。

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    MainViewController *vc = [storyboard instantiateInitialViewController];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    
    Run Code Online (Sandbox Code Playgroud)

这解决了黑屏问题。(首先,在注释中尝试初始化UITableViewController而不是MainViewController的错误。其次,当您使用时[viewController alloc] init],您创建了一个新的“ viewController”实例,而不使用情节提要。)

  1. 您的init方法将不会使用情节提要方式调用。因此viewDidLoad,请致电init。不过,我希望将其更改/重命名为loadData。

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
        [self init];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
    }
    
    Run Code Online (Sandbox Code Playgroud)

我在模拟器上看到了你想要的东西。