iOS tableView没有填充数据

时间:2014-03-29 18:25:27

标签: ios uitableview

有人可以向我解释为什么这不会在我的viewController中填充我的tableView吗?我可以在UITableViewController中使它工作,但是当我创建一个带有tableView的UIViewController时,它不会填充数据。在我的故事板中,我创建了一个带有tableview和表视图单元的视图控制器。当我运行程序时,我得到的是我的空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

的.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

3 个答案:

答案 0 :(得分:4)

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

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

编辑:

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

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

  1. 在AppDelegate.m中,使用以下代码,因为您使用的是storyboard(在我的情况下是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];
    
  2. 这解决了黑屏问题。 (首先,您在注释中尝试初始化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;
      }
      
    2. 我正在模拟器上看到你想要的东西。

答案 1 :(得分:1)

除了对方所说的,从故事板:

您应该通过按住命令UITableView链接到tableView属性,然后将其拖放到属性上:

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

使用UITableViewController时默认情况下会这样做。使用UIViewController时必须实现所有内容。因此,请确保将IBOutletsdelegatedatasources全部联系起来。

答案 2 :(得分:0)

我看不到

[self.tableView reloadData]; 

只有在调用此方法时,表格及其值才会被渲染。