2012-08-15 35 views
-1

我有这个基本的购物清单应用程序,在第一个视图中有两个文本字段。一个是物品名称,另一个是物品数量。一旦用户键入这两个条目并按下保存按钮,应用程序就会跳转到表视图控制器,其中条目首先存储在名为item的对象中,然后将该对象存储到项目数组中。然后,我实现所有必需的表视图方法,并将项目显示在表视图中。可悲的是,没有任何显示。当我保存一个条目时,表格视图显示为空白。在用NSLog的一些调试之后,我发现没有调用至关重要的方法tableviewcellForRowAtIndexPath。我发现其他人有一个类似的问题,并且解决方案是将tableviewcontroller设置为委托,我尝试过,但它不起作用。你知道我的代码有什么问题吗?谢谢。tableviewcellForRowAtIndexPath没有被调用

这里是我的第一个图像视图控制器,然后我会告诉你我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

已经做你的iboutlet的表视图和设置代表表视图 – parag 2012-08-15 18:17:17

回答

2
+0

我该怎么做? – user1601259 2012-08-17 20:18:41

+0

与设置委托完全相同,您的控制器需要实现,然后像您用于连接.delegate属性,您需要连接.datSource属性与您的视图控制器 – 2012-08-18 15:49:49

+0

它仍然不起作用 – user1601259 2012-09-07 23:21:33