所以我创建了一个子类UITableViewController
并将委托和数据源设置为自身。但仍然像这样的方法cellForRowAtIndexPath
永远numberOfRowsInSection
不会被调用。我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"];
}
当我尝试在弹出窗口中加载它时,我得到的只是一个空白弹出窗口。
我在这方面花了很多时间。请帮帮我。
编辑:.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