UITableViewController没有加载数据

时间:2012-08-20 05:17:18

标签: ios uitableview

所以我创建了一个UITableViewController的子类,并将委托和数据源设置为自身。但是仍然会调用cellForRowAtIndexPathnumberOfRowsInSection之类的方法。我numberOfSectionsInTableView每次都会返回1。 这就是viewDidLoad()的样子

- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray* list = [[NSMutableArray alloc] init];
self.tableView = [[UITableView alloc] init];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.actionList = list;
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 400.0);
[self.actionList addObject:@"t1"];
[self.actionList addObject:@"t2"];
}

当我尝试在弹出框中加载它时,我得到的是一个空白的弹出框。 This is what I see in the UIPopover

我花了很多时间在这上面。请帮帮我。

编辑:.m文件

// KLActionsViewController.m     #import“KLActionsViewController.h”

@interface KLActionsViewController ()

@end

@implementation KLActionsViewController
@synthesize actionList,delegate;

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

//- (void)loadView
//{
//    
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray* list = [[NSMutableArray alloc] init];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 100.0) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.actionList = list;
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 200.0);
    [self.actionList addObject:@"t1"];
    [self.actionList addObject:@"t2"];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"in number of section");
    return 1;
}

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSString* lab = [self.actionList objectAtIndex:indexPath.row];
//    NSLog(lab);
    NSLog(@"here in there");
    cell.textLabel.text = lab;
    return cell;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Total entries : %i ",[self.actionList count]);
    return [self.actionList count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate != nil) {
        [self.delegate actionSelected:indexPath.row];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.actionList = nil;
    self.delegate = nil ;
    // Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

.h文件

#import <UIKit/UIKit.h>

@protocol KLActionsViewControllerDelegate
- (void)actionSelected:(NSInteger)index;
@end

@interface KLActionsViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray* actionList;
@property (nonatomic, assign) id<KLActionsViewControllerDelegate> delegate;


@end

4 个答案:

答案 0 :(得分:0)

尝试在viewDidLoad

的末尾添加[self.tableView reloadData]

答案 1 :(得分:0)

如果要扩展UITableViewController它已在viewDidLoad创建UITableView实例。尝试在那里设置断点并在调试器控制台中检查它:

po [self view]

因此,此时您不应自行创建UITableView实例。如果您需要表视图的自定义初始化,则应该提前完成

修改

来自文档:

About Table Views in iOS-Based Applications

创建表视图的最简单和推荐的方法是创建自定义UITableViewController对象。 UITableViewController创建表视图并将自己指定为委托和数据源。

Creating a Table View Programmatically

如果您选择不使用UITableViewController进行表视图管理,则必须复制此类为您提供的“免费”。

- (void)loadView // <-- NOT VIEW DID LOAD!
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
                                  style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView reloadData];

    self.view = tableView;
    [tableView release];
}

答案 2 :(得分:0)

在.h文件中,添加UITableViewDelegateUITableViewDataSource

这样的事情:

@interface Yourclass : rootClass <UITableViewDelegate, UITableViewDataSource>

答案 3 :(得分:0)

在.h文件中尝试这个

@interface KLActionsViewController:UIView <UITableViewDelegate,UItableViewDataSource>
相关问题