UITableViewController不加载数据 [英] UITableViewController not loading data
问题描述
因此,我创建了UITableViewController
的子类,并将委托和数据源设置为其自身.但是像cellForRowAtIndexPath
和numberOfRowsInSection
这样的方法永远都不会被调用.我的numberOfSectionsInTableView
每次都会返回1.
这就是viewDidLoad()的样子
So I have created a subclass of UITableViewController
and set the delegate and datasource to itself. But still the methods like cellForRowAtIndexPath
and numberOfRowsInSection
never get called.My numberOfSectionsInTableView
return 1 everytime.
This is how viewDidLoad() looks like
- (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"];
}
当我尝试将其加载到弹出窗口中时,我得到的只是一个空白的弹出窗口.
When I try to load this in a popover all I get is a blank popover.
我花了很多时间在这上面.请帮帮我.
I have spent a lot of time on this. Please help me out.
.m文件
//KLActionsViewController.m #import"KLActionsViewController.h"
// 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
推荐答案
如果要扩展UITableViewController
,它已经在viewDidLoad上创建了UITableView
实例.尝试在那里设置一个断点并在调试器控制台中检查它:
If you are extending the UITableViewController
it already creates UITableView
instance at viewDidLoad. Try to set a breakpoint there and check it in debugger console:
po [self view]
因此,此时您不应该自己创建UITableView
实例.如果您需要表格视图的自定义初始化,则应该更早完成
Therefore you should not create the UITableView
instance by yourself at this point. If you need a custom init of the table view you should do it earlier
编辑
从文档中:
创建表视图的最简单且推荐的方法是创建自定义UITableViewController对象. UITableViewController创建表视图并将其自身分配为委托和数据源.
如果选择不使用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];
}
这篇关于UITableViewController不加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!