3

以下是我在屏幕上看到的内容(我试图展示我在屏幕上看到的内容)

++++++++++++++++++++++++++++++++++
+                                +
+  TextField-Name                +
+                                +
+  Save Button                   +
+                                +
+  UITableView                   +
+  .                             +
+  .                             +
+  .                             +
+                                +
++++++++++++++++++++++++++++++++++

当我单击保存按钮时,数据将被存储,并且我在下面的表格视图中显示相同的内容。问题是当点击保存时,数据被保存,但它没有显示在UITableView. 我相信[self.myTableView reloadData];会做的伎俩,但它没有做。

下面是我的代码。

.h 文件

#import <UIKit/UIKit.h>

@interface DeviceDetailViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (strong, nonatomic) IBOutlet UITableView *myTableView;

- (IBAction)save:(id)sender;

@end

.m 文件

@interface DeviceDetailViewController ()
@end

@implementation DeviceDetailViewController
@synthesize myTableView;

- (IBAction)save:(id)sender {
    // code for saving data
    NSLog(@"saved data and now reload data.... started....");
    [self.myTableView reloadData];
    // I thought this statement will do magic... but its not....
    NSLog(@"saved data and now reload data.... finised....");
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSLog(@"numberOfRowsInSection %d", self.devices.count);
    return self.devices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cel3";
    UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
    NSLog(@"cellForRowAtIndexPath %@ %@ by %@", [device valueForKey:@"name"], [device valueForKey:@"version"], [device valueForKey:@"company"]);
    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [device valueForKey:@"name"], [device valueForKey:@"version"]]];
    [cell.detailTextLabel setText:[device valueForKey:@"company"]];

    return cell;
}

当我运行应用程序时,下面是我得到的NSLOG

2012-12-31 11:52:34.222 MyStore[720:11603] numberOfRowsInSection 0
2012-12-31 11:52:34.627 MyStore[720:11603] numberOfRowsInSection 2
2012-12-31 11:52:34.628 MyStore[720:11603] cellForRowAtIndexPath iPhone 4s by Apple
2012-12-31 11:52:34.629 MyStore[720:11603] cellForRowAtIndexPath X10 Mini Pro by Sony

当我添加一些东西时,我得到

2012-12-31 11:53:21.868 MyStore[720:11603] saved data and now reload data.... started....
2012-12-31 11:53:21.869 MyStore[720:11603] numberOfRowsInSection 2
2012-12-31 11:53:21.870 MyStore[720:11603] saved data and now reload data.... finised....
2012-12-31 11:53:21.871 MyStore[720:11603] cellForRowAtIndexPath iPhone 4s by Apple
2012-12-31 11:53:21.872 MyStore[720:11603] cellForRowAtIndexPath X10 Mini Pro by Sony

这意味着所有方法都被调用,但我仍然没有获取数据。

有人可以指出我需要编写哪些额外的代码吗?

4

2 回答 2

1

因为你的table dataSource数组是self.devices数组。

原因可能是self.devices数组doesnot获取updatedsaving data

update self.devices那么首先reload table

于 2012-12-31T08:21:27.510 回答
0

我认为您的方法有问题cellForRowAtIndexPath尝试使用以下代码行

**

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

**

代替

static NSString *CellIdentifier = @"Cel3";
    UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
于 2012-12-31T08:41:30.480 回答