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

时间:2013-11-23 12:29:30

标签: ios objective-c uitableview

我正在使用表格视图来显示数据数组,我想传递该值以更新详细视图控制器中的标签和图像视图。我实际上创建了一个自定义单元格,我在表格视图中加载它,在选择一行时,我推出了一个带有标签和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];
 }

TrafficTableViewViewController.m

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

在上面的代码中TrafficDetailViewController是我创建的自定义视图控制器,它包含标签和UIImage。请让我知道我哪里出错...

3 个答案:

答案 0 :(得分:2)

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

- (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

时,还要编辑一件事

然后请将图像分配给trafficImage.image而不是trafficImage和字符串到LocationLabel.text,因为trafficImage是ImageView而不是Image而LocationLable是一个标签而不是string.So用以下代码替换你的行

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

POST EDIT

<。>文件中的

@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
<。>文件:

@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,我认为这会导致问题。请试试这个,让我知道它现在有效吗?

享受编码!!

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

您的问题可能与使用alloc init实例化TrafficDetailViewViewController的方式有关。这将创建一个具有空视图的控制器,可能不是您要在屏幕上推送的控制器。如果你在xib中创建了这个控制器的视图,那么你应该使用initWithNibName:bundle:而不是init。如果您在故事板中创建它,那么您应该使用[self.storyboard instantiateViewControllerWithIdentifier:@“MyIdentifier”]。这条线也可能是一个问题,我不知道你在这里要做什么:

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

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