Ios 将数据从表视图传递到详图视图时未更新详图视图

Ios 将数据从表视图传递到详图视图时未更新详图视图,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在使用一个表视图来显示一个数据数组,我希望将该值传递给“细节视图”控制器中的“更新标签”和“图像视图”。实际上,我创建了一个自定义单元格,并将其加载到表视图中,在选择一行时,我使用标签和ImageView推送了一个新的自定义视图控制器。我想使用要在自定义视图控制器的标签中显示的选定单元格的值。代码可以编译,但唯一的问题是细节视图中的标签和图像视图不会得到更新。我只是不知道我会错在哪里…我很长一段时间都在坚持。下面是我的应用程序中的代码 code in table view… - (NSIn

我正在使用一个表视图来显示一个数据数组,我希望将该值传递给“细节视图”控制器中的“更新标签”和“图像视图”。实际上,我创建了一个自定义单元格,并将其加载到表视图中,在选择一行时,我使用标签和ImageView推送了一个新的自定义视图控制器。我想使用要在自定义视图控制器的标签中显示的选定单元格的值。代码可以编译,但唯一的问题是细节视图中的标签和图像视图不会得到更新。我只是不知道我会错在哪里…我很长一段时间都在坚持。下面是我的应用程序中的代码

code in table view…

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tableData count]; 
 }
   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


if (cell == nil)
{


    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

}

cell.LocationLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.TimeLabel.text = [time objectAtIndex:indexPath.row];
return cell;
   }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];

trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
 //transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
 }
TrafficTableViewController.m

   @synthesize trafficImage = _trafficImage;
   @synthesize LocationLable = _LocationLable;

在上面的代码中,TrafficDetailViewController是我创建的自定义视图控制器,它包含标签和UIImage。请告诉我哪里出了问题…

更改创建和推送trailsController的代码顺序。创建它,设置属性,然后推送它


这可能是也可能不是你问题的原因。接下来,您需要调试trailsController。您如何处理属性trafficImage和locationTable?您是否有自定义设置器,或者在viewDidLoad或ViewWillAspect方法中使用它们?在其他视图控制器中发布这些方法的相关代码。

更改创建和推送trailsController的代码顺序。创建它,设置属性,然后推送它


这可能是也可能不是你问题的原因。接下来,您需要调试trailsController。您如何处理属性trafficImage和locationTable?您是否有自定义设置器,或者在viewDidLoad或ViewWillAspect方法中使用它们?在其他视图控制器中发布这些方法的相关代码。

尝试此代码,可能会解决您的问题。问题是您在按下控制器后设置了属性,这是错误的。请尝试以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
    trailsController.trafficImage = [thumbnails objectAtIndex:indexPath.row];
    trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];
    //transfering control to detail view
    [[self navigationController] pushViewController:trailsController animated:YES];
 }
编辑

.h文件 在.m文件中

如果尚未添加,请添加合成器

@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
当您尝试推送TrafficDetailViewController时,还要编辑一件事

然后请将image分配给trafficImage.image不分配给trafficImage,将string分配给LocationLabel.text,因为trafficImage是一个图像视图而不是图像,LocationLable是一个标签而不是字符串。因此,请用以下代码替换您的行

trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
后期编辑

在.h文件中

@interface TrafficDetailViewViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIImageView *trafficImage;
@property (strong, nonatomic) IBOutlet UILabel *LocationLable; 
//Add these two more properties in TrafficDetailViewController
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSString *locationString;

@end
在.m文件中:

@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
@synthesize image = _image;
@synthesize locationString = _locationString;
在viewDidLoad方法中:

- (void) viewDidLoad
{
    self.trafficImage.image = _image;
    self.LocationLabel.text = _locationString;
}
然后在前面的viewController中,您已经为TrafficDetailviewController编写了推送代码,请用您的代码替换以下代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
    trailsController.image = [thumbnails objectAtIndex:indexPath.row];
    trailsController.locationString = [tableData objectAtIndex:indexPath.row];
    //transfering control to detail view
    [[self navigationController] pushViewController:trailsController animated:YES];
 }
这背后的原因是您的ViewController在按下之前没有加载,我们正在尝试设置imageView.image和Lable.text,我认为这是导致问题的原因。请试试这个,让我知道它现在起作用了吗


享受编码

试试这段代码,也许它能解决你的问题。问题是您在按下控制器后设置了属性,这是错误的。请尝试以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
    trailsController.trafficImage = [thumbnails objectAtIndex:indexPath.row];
    trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];
    //transfering control to detail view
    [[self navigationController] pushViewController:trailsController animated:YES];
 }
编辑

.h文件 在.m文件中

如果尚未添加,请添加合成器

@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
当您尝试推送TrafficDetailViewController时,还要编辑一件事

然后请将image分配给trafficImage.image不分配给trafficImage,将string分配给LocationLabel.text,因为trafficImage是一个图像视图而不是图像,LocationLable是一个标签而不是字符串。因此,请用以下代码替换您的行

trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
后期编辑

在.h文件中

@interface TrafficDetailViewViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIImageView *trafficImage;
@property (strong, nonatomic) IBOutlet UILabel *LocationLable; 
//Add these two more properties in TrafficDetailViewController
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSString *locationString;

@end
在.m文件中:

@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
@synthesize image = _image;
@synthesize locationString = _locationString;
在viewDidLoad方法中:

- (void) viewDidLoad
{
    self.trafficImage.image = _image;
    self.LocationLabel.text = _locationString;
}
然后在前面的viewController中,您已经为TrafficDetailviewController编写了推送代码,请用您的代码替换以下代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
    trailsController.image = [thumbnails objectAtIndex:indexPath.row];
    trailsController.locationString = [tableData objectAtIndex:indexPath.row];
    //transfering control to detail view
    [[self navigationController] pushViewController:trailsController animated:YES];
 }
这背后的原因是您的ViewController在按下之前没有加载,我们正在尝试设置imageView.image和Lable.text,我认为这是导致问题的原因。请试试这个,让我知道它现在起作用了吗


享受编码

您的问题可能在于使用alloc init实例化TrafficDetailViewController的方式。这将创建一个具有空视图的控制器,并且可能不是您要在屏幕上推送的控制器。如果您在xib中创建了此控制器的视图,那么应该使用initWithNibName:bundle:而不是init。如果是在情节提要中创建的,则应使用[self.storyboard instanceeviewcontrollerwhiteIdentifier:@“MyIdentifier”]。这句话也可能是个问题,我不知道你在这里想做什么:

trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];

LocationLable是标签还是字符串?要填充标签,应将字符串传递给下一个控制器,并让该控制器设置其自己标签的文本。

您的问题可能在于使用alloc init实例化TrafficDetailViewController的方式。这将创建一个具有空视图的控制器,并且可能不是您要在屏幕上推送的控制器。如果您在xib中创建了此控制器的视图,那么应该使用initWithNibName:bundle:而不是init。如果是在情节提要中创建的,则应使用[self.storyboard instanceeviewcontrollerwhiteIdentifier:@“MyIdentifier”]。这句话也可能是个问题,我不知道你在这里想做什么:

trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];

LocationLable是标签还是字符串?要填充标签,您应该将字符串传递给下一个控制器,并让该控制器设置其自己标签的文本。

很抱歉,我没有获得第一部分。请您详细解释一下,我仍然是初学者…@Duncan希望您移动此行:[[self-navigationController]pushViewController:trailsController动画:是];之后是:trailsController.LocationLable=[tableData objectAtIndex:indexath.row];除了为imageView和标签连接已创建的插座外,我对detail viewController没有做太多的工作…@greg感谢您的解释…;)但它仍然没有得到你