我有这个基本的购物清单应用,在第一个视图中有两个文本字段。一个是项目的名称,另一个是项目quantitiy。一旦用户键入这两个条目并按下保存按钮,应用程序将切换到表视图控制器,其中条目首先存储在名为item的对象中,然后将对象存储到项目数组中。然后,我实现了所有必需的表视图方法,并将项目显示在表视图中。可悲的是,没有任何东西在展示。保存条目时,表格视图显示为空白。在对NSLog进行一些调试之后,我发现没有调用关键方法tableviewcellForRowAtIndexPath。我发现其他人有一个类似的问题,解决方案是将tableviewcontroller设置为委托,我试过但它没有用。你知道我的代码有什么问题吗?感谢。
这是第一张图片的视图控制器,然后我将向您展示我的tableviewController代码
#import "TVViewController.h"
#import "TableViewController.h"
//#import "TableViewController.m"
@implementation TVViewController
@synthesize title;//one of the text fields
@synthesize subtitle;//the other text field
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Save"]) {
NSString *string1 = self.title.text;
NSString *string2 = self.subtitle.text;
[segue.destinationViewController addName: string1 andNumber:string2];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setTitle:nil];
[self setSubtitle:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
这是我的viewcontroller 这是我的表视图控制器,忽略那些用于调试目的的nslog
#import "TableViewController.h"
#import "Item.h"
@implementation TableViewController
@synthesize items;
-(void) addName:(NSString *) name
andNumber:(NSString *) numberOfItems
{
Item *item = [[Item alloc] init];
item.itemName = name;
item.itemQuantity = numberOfItems;
[self.items addObject:item];
NSLog(@"Data has been passed");
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"VIew is ready");
//[self.tableView reloadData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"list initialized by count");
// Return the number of rows in the section.
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Item in cart";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Item *item = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = item.itemName;
cell.detailTextLabel.text = item.itemQuantity;
return cell;
}
答案 0 :(得分:2)
据我记得你需要为此设置tableVeiw的dataSource,delegate用于接收事件