【发布时间】:2014-01-08 19:09:42
【问题描述】:
在我的应用程序中,我正在使用情节提要。在情节提要中,我使用默认视图控制器进行登录并创建了另一个视图控制器名称 Inventory。在新的视图控制器中,我使用的是表格视图。该表正在显示,但未调用委托和数据源方法。我尝试了很多解决方案,但我无法理解问题。我使用的代码是:
#import <UIKit/UIKit.h>
@interface InventoryViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end
实现文件为:
#import "InventoryViewController.h"
#import "customCell.h"
@interface InventoryViewController ()
@end
@implementation InventoryViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 110, 320, 350)];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
[table relaodData];
}
- (IBAction)Logout:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"hello");
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customcell"];
if (cell == nil)
{
cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"customcell"];
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
cell.item_image.image = [UIImage imageNamed:@"ipad_strip_logo.png"];
cell.type.text = @"electronics";
cell.name.text = [NSString stringWithFormat:@"my object %d",indexPath.row];
return cell;
}
@end
【问题讨论】:
-
运行项目时日志中是否打印了“hello”?
-
@Ashu no hello 不打印,单元格数也不是 10..
标签: ios objective-c uitableview